I have a bunch of affiliate banners, that normally I would give the url to in the img src attribute, but I would like to save them locally, so that the page will load a little faster.

I can loop through the list of url's I have no problem, but how do I achieve the effect or right-clicking and "save target as" with PHP?

Thanks

    I'm not exactly sure what you're after here, but if you're wanting to parse through a list of image URL's in a script and have it download each image to the local filesystem, I would say it's as easy as looping through the URL's while using [man]file_get_contents/man to get the data and [man]file_put_contents/man to store it somewhere.

      It's not quite working, and I think the problem is the path to write the contents to. Maybe you can help me out:

      $i = 1;
      while ($row = mysqli_fetch_assoc($result)) {
      
      $thumb = file_get_contents($row['small_thumb']);
      
      $name = 'sml-' . $row['prodid'];
      
      file_put_contents("/images/thumbs/{$name}.jpg", $thumb);
      
      $i++;
      }
      
      echo "$i thumbs saved";

      The goal is to open the file that will be something like www.remote.com/images/111.jpg and save it to www.local.com/images/thumbs/sml-111.jpg

      I tried using a full path http://www.local.com... in the file_put_contents function but got an http error saying that http wrapper does not support writable connections, and through searching online, some suggest using realpath(), or something similar to do with the path of where I'm saving the file. What I don't understand about realpath() is how I specify the path of the directory I'm not in.

      Does the script I'm using this code in have to be in the same folder as where I want to save the images?

      Is everything else right, aside from specifying where I want the images saved?

      Thanks for your help!

        The script doesn't have to be in the same folder, but you do have to use the correct local path. You can't normally "write" data across an HTTP connection (for obvious reasons - you don't want the world uploading files to your webserver, right?), so you need to use a local path on the server itself.

        As it is now, you're telling PHP to look for a folder called "images" in the root of the server's hard drive. I doubt very much that such a folder exists, and that perhaps you meant the root of your website is where PHP should look. In this case, you'd want something like:

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/images/thumbs/$name.jpg");

          I think that worked, but now I get this error:

          failed to open stream: Permission denied in /var/www/vhosts/xxxx.com/httpdocs/admin/save_thumbs.php on line 23

          where line 23 is:

          file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/images/thumbs/{$name}.jpg", $thumb);

          Is this related to my own access on my own virtual-dedicated server, or is this related to the file I'm trying to access?

          Is there any way around this, or is my host just not going to let me do it?

          Thanks again

            What's the beginning part of that error message? Most likely it's saying that PHP can't get write access to the "thumbs" folder - try chmod'ing that folder to 777.

              Here's the whole message:

              Warning: file_put_contents(/var/www/vhosts/xxxx.com/httpdocs/images/thumbs/sml-457350423.jpg) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/vhosts/xxxx.com/httpdocs/admin/save_thumbs.php on line 23

              and it ouputs this for each pass of the loop.

              What do you mean by "chmod'ing that folder to 777" - I don't understand any part of that.

              Thanks

                First off, does the folder /var/www/vhosts/xxxx.com/httpdocs/images/thumbs/ exist already? If not, you need to create it first.

                Good2CU wrote:

                What do you mean by "chmod'ing that folder to 777" - I don't understand any part of that.

                Although it varies according to how the server was setup, a folder must have certain permissions that allow PHP to write data to that folder, not just read from it. "chmod'ing" refers to using a Unix program called "chmod" to change file/directory permissions. A mask/permission of 777 means that a given file/directory is readable/writable/executable by all users.

                If you don't have shell access and instead use an FTP program, you might see the permissions listed as such - "Read", "Write", "Execute" - for three different groups, ex. Owner, Group, and Others.

                  I was able to change the read/write/execute in Dreamweaver.

                  Thanks so much for your help, I would have never gotten this otherwise!

                    Write a Reply...