Jump to content

Help with random file


sac0o01

Recommended Posts

I am creating an image file through GD.  It successfully creates the file with a random file name. 

//define file name
$rand = rand(1,100000);
$filename = 'temp/' . $rand . '.png';

 

Upon the script completion it directs back to the index page where the image should be displayed but it does not.

<img src="<?php echo 'temp/' . $rand . '.png';?>" />

 

I was using $PHPSESSID to create and display the image and it worked fine except that IE held the image in cache and required a hard refresh to display the new image.

 

Any suggestions how to get the random image src working, or maybe a better way of handling the entire process??

 

Link to comment
Share on other sites

you're probably better off using time() than rand(),

this will return a unix timecode (seconds passed since 1970, jan 1st), so it will change every second.

 

//define file name
$filename = 'temp/' . time() . '.png';

 

and for the index page script which displays the image, how is the $rand variable being passed from the upload script to the index page?!

you may need to store it in the session, and IE shouldn't hold the image in the cache if it has a new file name?! Don't quote me on this however, sounds like something IE would do.

 

Link to comment
Share on other sites

Upon submit the page runs script.php and then uses  header("Location....") to return to the index page.  I am afraid this is causing the set variable to be wiped out. 

 

I may have to stick with the session named file and find a way to force refresh each time the form is changed.

 

 

Link to comment
Share on other sites

either that or you can pass the variable in the URL,

i.e.

//define file name
$filename = 'temp/' .time() . '.png';

$urlFile = urlencode($filename);
header("index.php?img=$urlFile");

 

then on index.php, to display image (providing all

if (isset($_GET['img']))
{
$imagePath = urldecode($_GET['img']);
echo "<img src='$imagePath' />";
}

 

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.