如何下载并保存远程服务器上的图片文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计178个文字,预计阅读时间需要1分钟。
php
$file_path=$dir . DIRECTORY_SEPARATOR . $filename; $fp=fopen($file_path, 'w'); curl_setopt($ch, CURLOPT_FILE, $fp);
$result=curl_exec($ch); $error=curl_error($ch); $http_code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); fclose($fp);
if ($error) { return false; } else { return $http_code; }}?>
下载远程图片/** * 下载远程文件 * @param string $url 网址 * @param string $filename 保存文件名 * @param integer $timeout 过期时间 * return boolean|string */ function http_down($url, $dir, $timeout = 60) { preg_match('/mmbiz_[\w]+/i', $url,$a); $ext = explode('_', $a[0])[1]; $filename = $dir.md5(time().mt_rand()).'.'.$ext; ini_set('php_curl','on'); $path = dirname($filename); if (!is_dir($path) && !mkdir($path, 0755, true)) { return false; } $fp = fopen($filename, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_exec($ch); curl_close($ch); fclose($fp); return $filename; }
本文共计178个文字,预计阅读时间需要1分钟。
php
$file_path=$dir . DIRECTORY_SEPARATOR . $filename; $fp=fopen($file_path, 'w'); curl_setopt($ch, CURLOPT_FILE, $fp);
$result=curl_exec($ch); $error=curl_error($ch); $http_code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); fclose($fp);
if ($error) { return false; } else { return $http_code; }}?>
下载远程图片/** * 下载远程文件 * @param string $url 网址 * @param string $filename 保存文件名 * @param integer $timeout 过期时间 * return boolean|string */ function http_down($url, $dir, $timeout = 60) { preg_match('/mmbiz_[\w]+/i', $url,$a); $ext = explode('_', $a[0])[1]; $filename = $dir.md5(time().mt_rand()).'.'.$ext; ini_set('php_curl','on'); $path = dirname($filename); if (!is_dir($path) && !mkdir($path, 0755, true)) { return false; } $fp = fopen($filename, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_exec($ch); curl_close($ch); fclose($fp); return $filename; }

