2

I need a php file with the ability to download multiple images per se from a folder on my server, with an unique ID per image.

e.g.

I have an image on my server: /home/example/public_html/multipleimage/2.png

And I want to download that image through the unique php file: http://example.com/file.php

I would type the following url: http://example.com/file.php?id=2 And the browser would have to return the image.

But... how should be the php file? Can it be done without going through a database?

Thanks.

2
  • What do you mean by download? Do you want the user to download the image? Or the server? Commented Feb 5, 2012 at 23:41
  • The server to download the image Commented Feb 5, 2012 at 23:44

2 Answers 2

4
<?php
header("Content-Type: image/png");
readfile(
    sprintf(
        '/home/example/public_html/multipleimage/%d.png', 
        $_GET['id']
    )
);

See the following entries in the PHP Manual:

Note that I am using sprintf with %d, so it will silently convert any non-numeric id value to 0, so a malicious input like ../../etc/passwd will try to read 0.png. If you want to use anything else but numbers, you need to sanitize the input to prevent directory traversal attacks and null byte poisoning (before PHP 5.3.4):

Sign up to request clarification or add additional context in comments.

Comments

0

I'm still not sure I know what you want but here it goes:

<?php
if (array_key_exists('id', $_GET) && is_numeric($_GET['id'])) {
    header("Content-Type: image/png");
    echo file_get_contents("/home/example/public_html/multipleimage/".$_GET['id'].".png");
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.