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

Link to comment
Share on other sites

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";

}
?>

Link to comment
Share on other sites

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....

}
?>

Link to comment
Share on other sites

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.................

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.