PHP - file_put_contents only saves half of the files?
I have no idea what's going on! Is there a limit on how many files can be saved per execution?
So, i have an array with a list of URLs to images on another server. That works as it should, and it returns valid URLs. The problem is when try to save them, only 52 files are saved when there are 100 URLs returned. As you see i echo out the imagepath before i save the image, and it after counting the lines it adds up to 100. (no, of course i didn't count them manually!)
I'm running a loop:
for ($i=0; $i <= $count; ++$i) {
$url = $response['images'][$i]['image'];
$path = 'exported/'.$response['images'][$i]['date'].'.jpg';
echo $response['images'][$i]['image'].'&l开发者_高级运维t;br />';
file_put_contents($path, file_get_contents($url));
}
Any ideas what's going on? Thanks a bunch!
Do some of the entries have the same value for $response['images'][$i]['date']
? You might be generating the same output filename ($path
) and have files overwriting each other. You can try echoing $response['images'][$i]['date']
to make sure
You are probably reaching the script timeout trying to download 100 images from PHP. As a scripting language, PHP is only supposed to run for 1-2000ms. However, basic PHP installs have script execution set at a whooping 30 seconds I believe, which probably isn't long enough for your script.
You may want to edit your php.ini file in /etc/php5/cgi/php.ini (may be in a different location in /etc/php[x]/xxx) and up the following settings:
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
memory_limit = 16M ; Maximum amount of memory a script may consume (16MB)
精彩评论