FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Image Download trough PHP?





Gushe
Hello all. Smile

I was just wondering; is there a way to Download Pictures trough PHP?
I was thinking about making an Image downloader for my Website; but I didn't find any functions that could help in this..
It just gotta be able Downloading a picture from it's source.

A Full script isn't needed; some hints on how to do it would be already a Great help! Smile

Thanx in Advance;
~ GuShe!
imagefree
are you looking to download more than 1 images? If yes, then you need to have either address (HTTP) of all images or an algorithm that can determine the addresses of the images.

If you are asking about it, tell me here.

Where to download? on local computer or on Server?
Gushe
I'm asking to just download 1 simple Image to my Computer. Smile
kv
Here you go

Code:

header("Content-Disposition: attachment; filename=\"".$filename."\"");
readfile($path.$filename);


$path is the path of the directory which contains image file (including the trailing slash)
$filename is the name of the image file
Gushe
Thanx mate! Smile

And Could you Explain the Script a little? It might always be helpfull to understand the Scripts I'm using. Smile

Cheers;
~ GuShe.
kv
Code:

header("Content-Disposition: attachment; filename=\"".$filename."\"");


This line forces the browser to offer download options and not just display image in the browser (or handle the file using default action depending on mime type)

Code:

readfile($path.$filename);


This line will read the contents of the file and sends it to the browser. Note that the file need not be under webroot and it still works. This feature can be used as security for your file downloads.
AOP Web Development
Just want add for more details direct downloading of the images.

you could try this code.

Code:

<?php
function force_download ($data, $name, $mimetype='', $filesize=false) {
    // File size not set?
    if ($filesize == false OR !is_numeric($filesize)) {
        $filesize = strlen($data);
    }

    // Mimetype not set?
    if (empty($mimetype)) {
        $mimetype = 'application/octet-stream';
    }

    // Make sure there's not anything else left
    ob_clean_all();

    // Start sending headers
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Transfer-Encoding: binary");
    header("Content-Type: " . $mimetype);
    header("Content-Length: " . $filesize);
    header("Content-Disposition: attachment; filename=\"" . $name . "\";" );

    // Send data
    echo $data;
    die();
}

function ob_clean_all () {
    $ob_active = ob_get_length () !== false;
    while($ob_active) {
        ob_end_clean();
        $ob_active = ob_get_length () !== false;
    }

    return true;
}
?>



I just search from the net. if you want to know more kindly visit the site

http://www.phpit.net/code/force-download/
Fire Boar
Or... if you just want the picture...

Right click -> Save Image As...

Or if the image is covered by a transparent gif, you can search the webpage source (Ctrl+U) for the <img /> tag that refers to the image.
Gushe
Well, I have some more questions about this.

Would there be a way to Download a whole bunch of image that are actually Not on your server at the same time? If you have the Image source of course.

If my website would be 'www.mywebsite.com', would I be able to get pictures from 'www.anotherwebsite.com/images/'? I'm talking about multiple pics; Example:

Code:
www.anotherwebsite.com/images/Image1.jpg
www.anotherwebsite.com/images/AnotherImage1.jpg
www.anotherwebsite.com/images/Image2.jpg
www.anotherwebsite.com/images/YetAnotherImage.jpg
www.anotherwebsite.com/images/AnotherUmage3.jpg


To download them all immediatly to your pc? Would there be like any way to do something like this? Smile


Thanx in Advance;
~ GuShe!
Gushe
Ok, now I changed plans.

I only have to be able to download 1 Image; BUT:

the script may not need the 'filename', because it will be different every time and quite hard to filer out of the image source.
The links will be similar to:

Code:
http://www.imageserver.com/images/oa/732/23732912.png


Does anyone has a small scriptie for this? Rolling Eyes


Thanx in Advance;
~ GuShe
rvec
Code:
<?php

$link = $GET['link'];

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image= curl_exec($ch);
curl_close($ch);
fclose($fp);

//now $image contains the image and you could show the image by doing this:

header('Content-Type: image/png');
echo $image;

//or you could save the image in a mysql database, or save it to a file.
?>


with the above script these two things should output the same:
Code:

<img src="http://www.imageserver.com/images/oa/732/23732912.png" />
and
<img src="http://url.to/script.php?link=http://www.imageserver.com/images/oa/732/23732912.png" />



if you want it to save to a file you should do that with curl directly. This can be done this way:
Code:
<?php
$link = $GET['link'];

$ch = curl_init($link);
$fp = fopen("image.png", "w");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>


or if you want to keep the image name use this instead of $fp = fopen("image.png", "w");
Code:

$name= explode("/", $link);
$num = count($name) - 1;
$filename = $name[$num];
$fp = fopen($filename, "w");
Gushe
Well,

Since I already have written a script that will receive the image sources for me, the only thing that I need left is the downloading itself.
Using explode I did manage to generate the path & the Filename.
Here's my script that will get $path and $filename:
Code:
     function DownloadPicture($link)
      {
      $imgname = explode("/", $link);
      $num = count($imgname) - 1;
      $filename = $imgname[$num];
      echo "$filename \t";   #Check if outputs correctly.
      $plength = strlen($link) - strlen($filename);
      $path = substr($link, 0, $plength);
      echo "$path <br>";    #Check if outputs correctly.
      }

It does output the path & filename correctly.
Though the downloading still won't work, using this:
Code:
header("Content-Disposition: attachment; filename=\"".$filename."\"");
readfile($path.$filename);



I'd prefer not to use cURL, Since it isn't present on all servers & I can't seem to run it here. Confused


Thanx in Advance;
~ GuShe.
Gushe
Ok,

So I managed to get my picture downloaded, but there's one little problem: It simply won't open.
If I try to view the picture in Windows Picture & Fax Viewer, or another image vieuwer, it won't give me a preview. Though when I open it with Photoshop it Does opens the picture.
I checked, and the image downloaded using the script has exactly the same size as the original.

Here's my script:
(I already stripped alot things away from what I had first, nothing worked Razz)
Code:

   function DownloadPicture($link, $name)
     {
         header("Content-Type: image/jpg");
         header("Content-Disposition: attachment; filename=\"" . $name . "\";" );
         readfile($link);
     }


Anyone knows how this could be..? Rolling Eyes

thanx in advance;
~ Gushe
Fire Boar
Try image/jpeg instead of image/jpg. Failing that, you could simply open it in photoshop and do "file->save as" and save it as a PNG. The change of format should make it readable.
Gushe
If I opened in Photoshop and saved as JPG with Photoshop it also opened.

Well, I'll try jpeg. Wink

-- Didn't work. Confused

-- Edit --
Got it working.

I forget to put a ob_clean_all() before I put the headers, otherwise wrong information from the buffer was added to the image file; so the import modulas couldn't be loaded.
All fixed now Wink

Thanx anyway;
~ GuShe
idkwtf
Thanks this has helped me alot
Related topics
PHP Toturail
PHP Tutorials
image gallery: suggesions reqd
Sending mail with flash using PHP
image drawing with php
Image Manipulation with PHP Script
PHP Image Questions
upload image through PHP AND AJAX
PHP Snippet
Image Editing (PHP)
php starts downloading, and will not load !solved!
php functions
IE8 not allowing new random image on each page (PHP and CSS)
PhP Image gallery?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.