The example of this article describes the method of PHP asynchronously writing the content (pictures or content) on the remote link to the local. Share it with everyone for your reference, the details are as follows:
/** * 异步将远程链接上的内容(图片或内容)写到本地 * * @param unknown $url * 远程地址 * @param unknown $saveName * 保存在服务器上的文件名 * @param unknown $path * 保存路径 * @return boolean */ function put_file_from_url_content($url, $saveName = 'tmp.png', $path = './Uploads/Tmp/') { // 设置运行时间为无限制 set_time_limit ( 0 ); $url = trim ( $url ); $curl = curl_init (); // 设置你需要抓取的URL curl_setopt ( $curl, CURLOPT_URL, $url ); // 设置header curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 运行cURL,请求网页 $file = curl_exec ( $curl ); // 关闭URL请求 curl_close ( $curl ); // 将文件写入获得的数据 $filename = $path . $saveName; $write = @fopen ( $filename, "w" ); if ($write == false) { return false; } if (fwrite ( $write, $file ) == false) { return false; } if (fclose ( $write ) == false) { return false; } return $filename; }
The above is the content of PHP's method to asynchronously write the content (picture or content) on the remote link to the local, more For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!