Jump to content

file_get_contents and copy not working...help please


~n[EO]n~

Recommended Posts

I have this code, it shows the images from one of my site and i want to copy it in my pc.. but file_get_contents is not working...

<?php
// fetch images
for ($i=11; $i<=50; $i++)
{
$var = $i.".jpg";
  	echo "<div>$var</div><br />";
  	echo "<div><img src=\"http://xxxxx.com/models/images/".$i.".jpg\"  /></div>";

// copy file
$file = file_get_contents("http://xxxxx.com/models/images/".$i.".jpg");
$newfile = "newfolder/".$var;

if (!copy($file, $newfile)) 
{
        echo "failed to copy $file...\n";
}

}
?>

 

Images are displaying fine, but copy is not working... Any help please

Thanks

Finally, I think this will work... but now i am getting another problem, it shows 5 images from 11.jpg to 15.jpg but it only copies the first one... and i get this error on other images..

 

Warning: stream_copy_to_stream() expects parameter 1 to be resource, boolean given in C:\wamp\www\freaks\test.php on line 20

but there is no line 20

 

<?php
// fetch images
for ($i=11; $i<=15; $i++)
{
$var = $i.".jpg";
  	echo "<div>$var</div><br />";
  	echo "<div><img src=\"http://xxxxx.com/models/images/".$i.".jpg\"  /></div>";

// copy file
$src = fopen("http://xxxxx.com/models/images/".$i.".jpg", 'r');
$dest2 = fopen("homio/".$var, 'w');
// copy through stream....
echo stream_copy_to_stream($src, $dest2) . " bytes copied \n";

}
?>

why not just try

 

<?php
// fetch images
for ($i=11; $i<=15; $i++)
{
$var = $i.".jpg";
  	echo "<div>$var</div><br />";
  	echo "<div><img src=\"http://xxxxx.com/models/images/".$i.".jpg\"  /></div>";

file_put_contents("homio/".$var,file_get_contents("http://xxxxx.com/models/images/".$i.".jpg"));
// copy through stream....

}
?>

First one gets copied nicely then below other images i get this error

 

Warning: file_get_contents(http://xxxxx.com/models/images/12.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Internal Server Error in C:\wamp\www\freaks\test.php on line 18 and same for other images 13.jpg... and so on

 

and line 18 is

file_put_contents.................

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.