【发布时间】:2015-02-19 08:49:46
【问题描述】:
我正在尝试将存储在 mysql 数据中的一堆图像文件链接从一台服务器复制到另一台服务器,这有点像在后台运行的 cron 作业,它检查外部链接并将其复制到服务器。手动查看时显示的链接很少,但是当我尝试通过 php 代码复制文件时没有任何反应。它只是输出空字符串。
以下是我尝试复制到我的服务器的几个链接。
- http://crm.propspace.com/watermark?c_id=1646&l_id=2300187&aid=1444765&id=14181282100524636&image=09_12_2014-16_53_34-1646-b15d08432d253ac710dc7bea47bf67c9.png
- http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_24-1646-67974878d8b7abd86d86c93527e15ebd.jpg
- http://crm.propspace.com/watermark?c_id=1646&l_id=2257894&aid=1444765&id=14169037278702926&image=25_11_2014-12_29_31-1646-0cf46efe5b2f89214001a2e050353736.jpg
- http://crm.propspace.com/watermark?c_id=1646&l_id=2282465&aid=1444765&id=14176053840492751&image=03_12_2014-16_36_29-1646-46cd5f89122e6899f8185e20bd942dd7.png
链接打开并显示图像,但是当我尝试通过 php 代码复制时,我得到的只是空数据。我尝试了所有类型的方法来复制图像,例如
1. copy()
2. fopen()
3. file_get_contents()
4. curl_init()
但一切都以空字符串或数据结束。谁能帮我解决这个问题。
用于检索图像文件的方法
$img_link = "image from link";
$upload_link = "image to save link";
Method 1
------------------------------------------------------------------------------
$img_cont = file_get_contents($img_link);
file_put_contents($upload_link, $img_cont);
Method 2 - (This method gives me an image with 0 bytes)
------------------------------------------------------------------------------
copy($img_link, $upload_link);
Method 3
------------------------------------------------------------------------------
$img_file = fopen($img_link, 'r');
$img_data = '';
while($chunk = fread($img_file, 8192))
$img_data .= $chunk;
fclose($img_file);
Method 4
------------------------------------------------------------------------------
$img_stream = fopen($img_link, 'r');
$img_data = stream_get_contents($img_stream);
fclose($img_stream);
Method 5
------------------------------------------------------------------------------
$referer = "http://www.propspace.com/"
$headers = array(
'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png',
'Connection: Keep-Alive',
'Content-type: application/x-www-form-urlencoded;charset=UTF-8'
);
// Initialize cURL Connection
$conn = curl_init();
// Set cURL Options
curl_setopt($conn, CURLOPT_URL, $img_link);
// Add Headers
if(is_array($headers) && count($headers) > 0){
curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
}
// Add References
if($reference != "")
curl_setopt($conn, CURLOPT_REFERER, $reference);
// Other Options
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_BINARYTRANSFER, true);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($conn, CURLOPT_FAILONERROR, true);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($conn, CURLOPT_HEADER, 0);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, 1);
// Execute Request
$img_cont = curl_exec($conn);
谢谢。
更新:刚刚发现了一个使图像显示为 0 大小的东西。它只能从迪拜或阿联酋网络访问。我怎样才能让它工作呢?
【问题讨论】:
-
你可以添加你的代码吗?
-
请检查一下我已经提到了我使用的方法...
-
如果您在将内容放在服务器上时遇到问题,则可能是权限问题,这意味着您必须为您尝试在服务器上保存图像的文件夹设置 777。
-
不,这不是问题...问题在于从我在上面的帖子中提到的某些链接中读取图像文件...
-
我无法在浏览器中访问它们。
标签: php mysql curl fopen file-get-contents