Jump to content

Recommended Posts

i have stored an image on the server with an random number attached to it.  to display the image again i would need to no thi random number.  to get round this i have stored the number appended to the file in a mysql db.  i then retrieve this number when i need to display the image again.  but this is not working i am unable to display the image again.  i know the number is being stored but i cannot get the image to display.  here is the ode i am using to display the image

 

$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 was stored on the server with this code

 

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

Link to comment
https://forums.phpfreaks.com/topic/105718-solved-displaying-images/
Share on other sites

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

this will mess it up, your getting all the records from the database but $rand with only be set to the number of the last record..

 

 

instead of storing just the number why not store the full file name ?

at the bottom of the code i changed the else to look like this

 

$broad_img1= no image found;

 

and that was printed on the screen.  could it have something to do with the value of $rand not having a value outside of the sql statement.

instead of storing just the number why not store the full file name ?

 

 

 

or try this update

 

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

$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))
   //surely it this way around
   if (file_exists($web_image_folder.'/'.$image_name.$rand.'.'.$ext))
   {
     $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 {
echo "Image extension not found";
    // error, we can't find the image.
}

?>

 

i assume the file still has the extension, can you post and example of the uploaded file name

 

the image still didnt display.  when the file is uploaded a thumbnail image is displayed and the property name of the image is

 

http://acmeart.co.uk/mercury/image/thumbs/thumb_image1.jpg69

 

will the variable $rand have a value still when it is used in the file exists or would it have to be defined as a global for the value to be carried over.

instead of storing just the number why not store the full file name ?

 

Okay you can't display an image (in html) without an image extension so you need to fix that first

 

 

will the variable $rand have a value still when it is used in the file exists or would it have to be defined as a global for the value to be carried over.

 

it will have the value pull from the database

 

 

once  the above is done, (i recommend saving the full image name in the database, but if you don't

 

try some debuging

foreach($exts as $ext)
{
echo "<br>DEBUG:".$web_image_folder.'/'.$image_name.$rand.'.'.$ext;
   if (file_exists($web_image_folder.'/'.$image_name.$rand.'.'.$ext))
   {
echo "~FOUND!";
     $image = $image_name.'.'.$ext . $rand;
   } 
   
}

 

i have now changed the code so that it stores the path to the image in the mysql db and got rid of the if file exists part.  the code to store the path now looks like this

 

$image_name=image1 . $rand.'.'.$extension;
$broad1name2="image/thumbs/thumb_".$image_name;
$sql="INSERT INTO random_number (random1) VALUES ('$broad1name2')";
$query = mysql_query($sql);

 

and the code to display the image looks like this

 

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

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

$path= 'http://www.acmeart.co.uk/mercury';
$broad_img1='<img src="' . $path."/".$image_path .'" />';

 

when the source is viewed where the image is supposed to be displayed i get this

 

<img src="http://www.acmeart.co.uk/mercury/" />

 

from this it looks like $image_path has no value held in it.

i changed the code and also added a file to truncate the table each time a new image is uploaded and the source file still just stops at the end of the path variable.  i no the image is stored on the server and is stored in the mysql db as i can display the image straight from the mysql statement but it will not show when the image is sent using this code.

 

the images are being added to an email.  from looking at the source it looks like when i set the path of the file from the mysql db to a variable when the variable is recalled later down the script it holds no value.

 

when i added the last piece of code you uploaded is showed only one file

i have just created a new page for testing when i put this code on to the page and the uploaded it to the server it displayed the image

 

					$query = "SELECT broad1 FROM images_broad";
				$result=mysql_query($query);

				while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
				echo '<img src="'.$row['broad1'].'"/>';

 

when i changed the code on the  page to this

 

$query = "SELECT broad1 FROM images_broad";
				$result=mysql_query($query);

				while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
				echo '<img src="'.$row['broad1'].'"/>';
				$name="{$row['broad1']}";
				}
print_r($name);

 

it displayed the image again and wrote this on the page

 

image/thumbs/thumb_image161.jpg

 

so the code works here so why not on the other page?????????????

<?php
$query = "SELECT broad1 FROM images_broad";
$result=mysql_query($query);

while($row=mysql_fetch_array($result)){
$image_path="{$row['broad1']}";
}

$path= 'http://www.acmeart.co.uk/mercury';
$broad_img1='<img src="' . $path."/".$image_path .'" />'; 




// construct mailing list array
$merc_list = explode(",",$merc_mailingList);

// deploy the emails
for($i=0; $i<count($merc_list); $i++){
$message = '<style type="text/css">body {	margin: 0;	background: ;	background-image: url();	background-repeat: no-repeat;
} .mainTxt {font-size: 11px; color:#444444; font-family: Arial, Helvetica, sans;} .whiteTxt {font-size: 9px; color:#FFFFFF; font-family: Arial, Helvetica, sans;} a:link {color:#EB7324} ul {font-size: 9px; color:#EB7324;padding-left: 5px;}
.style3 {font-family: Geneva, Arial, Helvetica, sans-serif; color: #FFFFFF; font-weight: bold; }
.style1 {font-family: Arial, Helvetica, sans-serif;	font-size: 10px;color: #666666;}
</style>';

$message .= '<body leftmargin="0" topmargin="0">
<table width="900" border="0" align="center" cellpadding="0" cellspacing="5">
    <tr>
        <td height="95" colspan="2" scope="col"><img src=http://www.acmeart.co.uk/mercury/layout/BroadsheetSample/broadhead.jpg width="900" height="150"/></td>
    </tr>
    <tr>
        <td height="25" colspan="2" scope="col"><span class="mainTxt"><strong>' . $broad_topictitle1 . '</strong></span></td>
    </tr>
    <tr>
        <th width="23%" height="200" scope="col"><div align="left">' . $broad_img1 .'
	<p> </p></div></th>
        <td width="77%" scope="col"><table width="99%" border="0" cellpadding="10" cellspacing="0">
            <tr>
                <td align="left" valign="top"><span class="mainTxt">' . $broad_messagebody1 . '</span></td>
                <!-- End Image 1 -->
            </tr>
        </table>
        <p> </p>
        <p> </p>
        <p> </p>
        <p> </p></td>
    </tr>
<tr>
        <td height="25" colspan="2" scope="col"><span class="mainTxt"><strong>' . $broad_topictitle2 . '</strong></span></td>
    </tr>
    <tr>
        <th width="23%" height="200" scope="col"><div align="left">' . $broad_img2 . '
	<p> </p></div></th>
        <td width="77%" scope="col"><table width="99%" border="0" cellpadding="10" cellspacing="0">
            <tr>
                <td align="left" valign="top"><span class="mainTxt">' . $broad_messagebody2 . '</span></td>
                <!-- End Image 1 -->
            </tr>
        </table>
        <p> </p>
        <p> </p>
        <p> </p>
        <p> </p></td>
    </tr>
    <tr>
        <td colspan="2" scope="col"><img src="http://www.acmeart.co.uk/mercury/layout/BroadsheetSample/broadfoot.jpg" width="900" height="150" /></td>
    </tr>
    
    <tr>
        <td height="13" colspan="2" scope="col"><div align="justify"><span class="style1">Linacre Voice is a monthly bulletin from LinacreOne Community Partnership 140-142 Linacre Road Litherland L21 8JU Tel: (0151) 922 4898 Open Monday - Thursday 10:00am - 4:00pm <font size="1"></font></span> </div></td>
    </tr>
</table>
</table>
</body>';



// Contacts
//$replyName = $merc_replyId;
//$replyEmail = $merc_replyAddress;

$replyName = "$merc_replyId";
$replyEmail = $merc_list[$i];

$contactname = "";
$contactemail = $merc_list[$i];

// Subject
$subject = $merc_messageTitle;

// Headers
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: ".$replyName." <".$replyEmail.">\r\n" . PHP_EOL;
$headers .= "To: ".$contactname." <".$contactemail.">\r\n" . PHP_EOL;


mail($contactemail, $subject, $message, $headers);
} // END for

?>

no i just want to kick the comp now that didnt work either.  the cource sode still just displays this

 

<img src="http://acmeart.co.uk/mercury/" />

 

where the images are meant to go the part of the path it pulls from the db just is not there.

try this,

do updates (see comments)

<?php
$query = "SELECT broad1 FROM images_broad";
$result=mysql_query($query);

while($row=mysql_fetch_array($result))
{
$image_path=$row['broad1'];
}
//Add line beow
$image_path= "thumbs/thumb_image161.jpg";

$path= 'http://www.acmeart.co.uk/mercury/image';//append the /image
?>

 

 

now if that works, its a problem with the database (i don't have the db so i can't test that)

that will work as the full path to the file is being coded in.  the reason i cannot do it like that though is because the extension to the file can change from jpg, gif, to png as the files that are uploaded have different file types this part needs to change.

 

i have had it working with the if file exists function in php but it kept on displaying the images from the cache if the extension didnt change.  so i have now had to add a random number to the file so this can not happen.

 

the original file name would have been thumb_image1 then either jpg, gif or png.  so if a gif was uploaded one day and then the next a different gif was uploaded it would still display the old one.  with the random number the file names are always different so it cannot be displayed from the cache.

 

the problem this then causes is that the random number must be stored and then recalled to display the file on different pages.

 

correct me if i am wrong but when the image is sent it looks for the variables and there values.  as the images are being viewed from within an email would the value held inside the image path variable still hold a value as this was taken from the mysql db.

the reason i hard coded it was because the database should return that (on this test), the fact the test worked proves the database isn't returning the correct data..

 

if the problem is purely a caching issule you can do this

 

 

<?php
$rand = time();
echo "<img src='myimage.jpg?$rand' >";
?>

i have added this line of code

 

$broad1name3="image/thumbs/thumb_".$image_name?$rand;

 

as this is the line of code used to display the thumbnail for the first time.  so underneath this is

 

$thumb_name=$broad1name3;

 

and when this code is executed

 

echo '<img src="'.$thumb_name.'">';

 

i get this error

 

Parse error: parse error, unexpected ';'

 

this is talking about the first line of code on this post.

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.