PHP implements asynchronous method to write the content (image or content) on the remote link to the local

黄舟
Release: 2023-03-06 06:42:01
Original
980 people have browsed it

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;
}
Copy after login


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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!