简体   繁体   English

使用php在本地服务器上保存远程图像文件,但未完全成功

[英]using php to save remote images files at local sever but not completely successfully

I know some people already ask it but my probelm is when downloading remote images files(each file size is smaller than 200KB). 我知道有人已经问过这个问题,但是我的探索是在下载远程图像文件时(每个文件的大小小于200KB)。 But some files won't be completly saved. 但是某些文件将无法完全保存。 And some files can not be saved, or some files are saved but not 100%, I will see some gray shadow at image. 并且某些文件无法保存,或者某些文件已保存但不是100%,我会在图像上看到一些灰色阴影。 The worst is everytime is different error ouput.(it's internet problem?) 最糟糕的是每次都有不同的错误输出。(这是互联网问题吗?)

I try the following methods to save file. 我尝试以下方法保存文件。

  1. file_get_contents file_get_contents
  2. curl/GD 卷曲/ GD
  3. copy 复制

it all can work but I can't find the perfect method to save whole files. 一切都可以,但是我找不到保存整个文件的完美方法。

The followings are error msg. 以下是错误消息。

  1. failed to open stream: HTTP request failed! 无法打开流:HTTP请求失败! HTTP/1.0 408 Request Time-out in at line "copy" HTTP / 1.0 408请求超时在“复制”行
  2. Maximum execution time of 60 seconds exceeded( I increase time) 超过60秒的最大执行时间(我增加了时间)

my php program. 我的PHP程序。

set_time_limit(60);

$imageArray=array(image array............);

for ($k=0;$k<count($imageArray);$k++){

echo '<img src="'.$imageArray[$k].'"><br/>';

$isok=copy($imageArray[$k] , dirname(__FILE__).'/photo/item_'.($k+1).'.jpg');

if(isok==true){
   echo' success!';  
}else{
  echo ' Fail';  
}

Most possibly it's an internet problem. 最有可能是互联网问题。 Do they load fine in the browser when you try ? 尝试时,它们是否可以在浏览器中正常加载? If they do, you can try running the code on your machine and see if this would help. 如果这样做,您可以尝试在计算机上运行代码,看看是否有帮助。

But most probable reason is the remote site which you try to download from - it can throttle you for connections per time-interval. 但最可能的原因是您尝试从其下载的远程站点-它可以限制您在每个时间间隔内的连接。 Try sleeping between images - for example 5-6 seconds and see if this helps. 尝试在图像之间休眠-例如5-6秒,看看是否有帮助。

Also try to download smaller batches of images - 1 - 2 at a time to see if it works. 另外,请尝试一次下载较小数量的图像-1-2-以查看其是否有效。

I noticed in your copy() that you have hardcoded in .jpg to the destination output. 我在您的copy()中注意到,您已将.jpg硬编码到目标输出中。 Are you always uploading .jpg as if you are uploading a .png or .gif and are forcing it to .jpg you might be causing issues there. 您是否总是上载.jpg,就像上载.png或.gif并将其强制为.jpg一样,可能会在其中引起问题。 Just a thought to be honest. 只是说实话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM