由于公司网站之前的用户头像都是存储在自己的服务器上的,后来感觉管理不方便,新增加的用户头像都上传到了七牛,为了方便统一管理,领导说把本地服务器的头像全部迁移到了七牛。
1.梳理下思路
先判断用户的头像是否在七牛,若不存在,本地如果有则抓取到七牛,然后进行批量抓取
2.七牛判断图片是否存在
/** * 查看七牛url是否存在 * @param string $url */ function url_exists($url) { require_once(COMMON_PATH."qiniu/rs.php"); require_once(COMMON_PATH."qiniu/http.php"); $parts=parse_url($url); $bucket ;//桶 $key=substr($parts['path'], );//七牛文件名 //密钥 $accessKey ; $secretKey ; Qiniu_SetKeys($accessKey, $secretKey); $client = new Qiniu_MacHttpClient(null); list($ret, $err) = Qiniu_RS_Stat($client, $bucket, $key);//查看单个文件信息 if ($err !== null) { return false; } else { return true; } }
3.移动单个文件到七牛
/** * 移动单个头像到七牛 * @param string $remotePath 远程文件在服务器上的物理地址 * @param string $qiniuPath 七牛文件地址 */ function oneFetch($remotePath,$qiniuPath){ if(!$this->url_exists($qiniuPath) && file_exists($remotePath)){ require_once(COMMON_PATH."qiniu/rs.php"); require_once(COMMON_PATH."qiniu/http.php"); require_once(COMMON_PATH."qiniu/conf.php"); $client = new Qiniu_MacHttpClient(NULL); /*$accessUrl 远程文件访问地址 *$bucket桶 *$key 存储在七牛的文件名 */ $ret = Qiniu_RS_Fetch($client,$accessUrl, $bucket, $key); if($ret != NULL) { $errlog = 'fail.log';//写错误日志 file_put_contents($errlog, "{$key}\n", FILE_APPEND); } else { var_dump($ret); } }else{ var_dump('已有头像'); } }
4.批量抓取
function t(){ header("Content-type: text/html; charset=utf-8"); set_time_limit(0); $start = intval($_GET['num']);//开始位置 $str = ''; /*从数据库查询记录*/ $link = mysql_connect("localhost", "root", "root") or die("error"); $db_selected = mysql_select_db("test", $link); mysql_query ("SET NAMES UTF8"); $limit = 10;//每次查询数 $sql = "SELECT * FROM test order by id desc limit {$start},{$limit}"; $result = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($result)) { while($row=mysql_fetch_assoc($result)){ oneFetch($row['remotePath'], $row['qiniuPath']); } $start += 10; $url = "/t?num={$start}"; $str = '已经更新'.$start.'条<br />'; $str .= '<script>location.href="'.$url.'";</script>';//更新$limit条,跳转防止脚本无响应 } else { echo '结束'; } echo $str; ob_flush(); flush(); sleep(2); unset($sql,$result,$row); }
以上内容是基于php实现七牛抓取远程图片的全部内容,希望大家喜欢。

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
