4

I don't want to show the real URL of images. So I am using uniqueid() to replace the URL of dynamic images and store the real URL against the uniqueid() in a session. So I will get a load of uniqueids => real-image-path.jpg in the session array. Then using the following code to echo out image:

<img src="getImage2.php?h=<?php echo $uniqueID; ?>" title="" >

That all seems to work. In getImage2.php, I'm wondering how I can use the variables stored in the session to echo out the images. I have got:

session_start();
header("Content-Type: image/jpeg");
while($_SESSION[$uniqueID]){
echo readfile($_SESSION[$uniqueID]);
}

But this looks pretty hopeless and obviously isn't working. What is the best way to echo the images in getImage2.php?

Sample of session array looks like this:

Array ( [start] => 1435057843 
        [start_date] => 23/06/15 
        [start_time] => 12:10:43 
        [b312a3f205176aa006c8712b3aedb2a4] => images/1370322222.jpg 
        [5311d8a77e3889723a61b3768faaa4df] => images/1357323650.jpg 
        [fa14a6a315bf7ddbeb7390af23467f5e] => images/1415737586.jpg 
        [dd624079e982d78e538f873b7719f179] => images/1369865823.jpg 
        [5c4011114f6abbb9ecaf2ffbe4d3936f] => images/1369885151.jpg 
        [d26e3a017ce4851a19511fc0dfedc595] => images/1370317410.jpg
        .............
1
  • In some cases you can do internal redirects, that would save up your PHP from having to serve the file; also, have a look at this answer for some inspiration. Commented Jun 23, 2015 at 11:41

3 Answers 3

0

You dont want to try and echo all the images at once. Each <img> tag will send an individual request to the server to be served a specific image as part of the page load, so just get the PHP code on the server to return the specific image requested like so :-

getImage2.php script

session_start();

$filename = 'images/missing_image.jpg'; // set a default

if ( isset($_GET['h']) && isset($_SESSION[ $_GET['h'] ]) ) {
    $filename = $_SESSION[ $_GET['h'] ]
}

header("Content-Type: image/jpeg");
readfile( $filename );

NOTE RE Your comment

As you are using this

<img src="getImage2.php?h=<?php echo $uniqueID; ?>" />

which will get written to the browser as something like this

<img src="getImage2.php?h=b312a3f205176aa006c8712b3aedb2a4" />

The $_GET array will look like this when getImage2.php received it.

$_GET [
         h => b312a3f205176aa006c8712b3aedb2a4
      ]

So $_GET['h'] will be what you are calling the uniqueID and you use that to address the correct file path by using it as the key to the $_SESSION array

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

7 Comments

isn't 'h' just the uniqueID(). The real image path is contained in session as uniqueID =>real-image-path.jpeg. So dies $_SESSION[$_GET['h']] = real image path?
See my NOTE re your comment
Thanks for your help and sorry for being dense here. How do I get the real image path which is = to the uniqueID()? Does the line readfile($_SESSION['GET['h']]); look at the current session and find the value of the uniqueID (taken from the img source) and then echo the real image path?
Yes it should, assuming I have made all the right assumptions about how you built the $_SESSION array data. Do a print_r($_SESSION) and edit your question and add a sample of that output so I can see what your $_SESSION actually looks like
Thanks RiggsFolly. Have edited question with sample of session array.
|
0

You should not use a while loop or readfile here. Instead, try this:

session_start();
header("Content-Type: image/jpeg");
echo file_get_contents($_SESSION['$uniqueID']);

I'm assuming you are trying to output one image at a time.

2 Comments

want to display all images on main page but get the real urls from getImage2.php. Do I not need to display all images on getImage2.php? Also - isn't file_get_contents more memory intensive than readfile()?
As @RiggsFolly mentioned, each img tag on your main page will send a request for one image to getImage2.php, so you only need to output one at a time. Using readfile is also fine, just not in a loop like in your question.
0

This should get you started along the right path:

session_start();
// get passed 'h' parameter and look it up in the session
$handle = isset($_GET['h']) ? $_GET['h'] : null;
$image = isset($_SESSION[$handle]) ? $_SESSION[$handle] : null;

if (file_exists($image) {
    // the file can be found
    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($image));

    session_abort(); // close session without changes
    readfile($image);
} else {
    // file was not found
    if ($image !== null) {
        // we should invalidate the session
        unset($_SESSION[$handle]);
    }
    header('HTTP/1.0 404 Not found');
}

Though, typically a better approach to this is by using X-Sendfile or X-accel headers; you can read more about this from my older answer.

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.