i have store an image on the server from a file upload with a random number attached to it. i have then stored the random number in a mysql db for use on other pages to display the images.

the problem i am having is that the image will not display on other pages when i try to recall it from the server.

i have stored the image in a folder on the server using this code

"image/thumbs/thumb_".$image_name . $rand;

$image_name being the name of the file to be stored and $rand being the random number. i have then stored the random number in the db using

$rand= rand(0, 100);
$sql="INSERT INTO random_number (random1) VALUES ('$rand')";
$query = mysql_query($sql);

to get the file from the server to be displayed the code i have used is this

$image = "";
$broad_img1= "";
$path= 'http://www.acmeart.co.uk/mercury/';
$web_image_folder = 'image/thumbs';
$exts = array('jpg', 'png', 'gif', 'jpeg');
$image_name = 'thumb_image1';


// check for each extension
foreach($exts as $ext) {
   if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext . $rand)) {
     $image = $image_name.'.'.$ext . $rand;
   } 
   
}


// check if we have an image
if ($image != "") {
    // ok we have the image
    $broad_img1='<img src="' . $path."/".$web_image_folder."/".$image . '" />'; 
} else {
    // error, we can't find the image.
}

this code worked fine before i was adding the random number. it retrieved the image and displayed the cached version (when cached cleared displayed new upload image).

i have got the random number out of the db using this code

$query = "SELECT random1 FROM random_number";
$result=mysql_query($query);

while($row=mysql_fetch_array($result)){
$rand="{$row['random1']}";

the number is then placed into the variable and then tried to be used to display the image but the image will not display. it may be the way i have written the code to retrieve the image but my eyes are tired of looking so i dont think i would see a mistake if it slapped me.

i will post the second half of the code in full below.

$query = "SELECT random1 FROM random_number";
$result=mysql_query($query);

while($row=mysql_fetch_array($result)){
$rand="{$row['random1']}";
}

$image = "";
$broad_img1= "";
$path= 'http://www.acmeart.co.uk/mercury/';
$web_image_folder = 'image/thumbs';
$exts = array('jpg', 'png', 'gif', 'jpeg');
$image_name = 'thumb_image1';


// check for each extension
foreach($exts as $ext) {
   if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext . $rand)) {
     $image = $image_name.'.'.$ext . $rand;
   } 
   
}


// check if we have an image
if ($image != "") {
    // ok we have the image
    $broad_img1='<img src="' . $path."/".$web_image_folder."/".$image . $rand'" />'; 
} else {
    // error, we can't find the image.
}

the image will not display, will the $rand variable still hold the value set from the mysql query.

Quick sanity check...
In your original post you first saved the image with a random value, then you generated a new value and inserted it in the database. How could you possibly match the file if you are using a new random number after you already wrote the file?

instead of

"image/thumbs/thumb_".$image_name . $rand;
$rand= rand(0, 100);
$sql="INSERT INTO random_number (random1) VALUES ('$rand')";
$query = mysql_query($sql);

try

$rand= rand(0, 100);
"image/thumbs/thumb_".$image_name . $rand;
$sql="INSERT INTO random_number (random1) VALUES ('$rand')";
$query = mysql_query($sql);

this way you use the same value in both the image and db query.

sorry for wrecking your head it is all the same code that is why i posted twice. i was showing the lines of code as i talked about them but didnt put them in the correct order. when i put the post up and looked at it i realised it didnt look right. i put it up in full so it could be read through more easily.

This article has been dead for over six months. Start a new discussion instead.