i am trying to get an image file to display in an email, the file is stored on the server but it will not send the file with the email. i have been working on this code now for a few days and i cannot get it to work.

the files that i need displaying change so i have incorporated an array to get the file extension, the email is sent with all the text and images which have been coded in using the absolute file path but the images i want to get from the if file exists function will no show.

here is the code for the file exist if anyone can see why this is not working please show me.

$image = "";
$web_image_folder = 'http://www.acmeart.co.uk/mercury/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)) {
     $image = $image_name.'.'.$ext;
   }
}

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

Hey Kevin,

Just to clarify, are you saying that the images are not found when executing the file_exists command?? If so, have you tried putting a relative file path in, instead of the absolute path?

Beyond that, I cannot see any issues with the code. I might be inclined to change the section if ($image != "") { for and use is_readable instead, with the relative path passed in again. Just because a file exists, it doesn't mean it is readable.

Best,
R.

the images are there weather it is finding them is another question i would need to put in some echo "file found" along the way to see that. but if you can not see a problem with that code then they must be getting found just not getting displayed.

as the images will be sent out in a email the absolute path to them must be used when displaying them. for the file exist part could the image file along with the extension be found using relative paths. would this section work if the relative paths where used.

when it comes to display the image then the absolute path would have be used to displayed the image as it is being sent out in an email.

the file extensions can only ever be gif, jpeg or png files, it there another way you know of i could get the image to display in the email.

i can upload the page which is to be sent out if that would make this problem clearer for you.

Hey Kevin,

Yeah, if you can upload some more content, perhaps the HTML email content, then that may help.

Best,
R

sorry about that just been having ago at the Norton consultants. they want £70 to remove a virus when i have already paid for my license cheeky bast***s. here is all the code for the send email page.

<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
if(!session_is_registered("first_name")){ // if session variable "username" does not exist.
header("location:index.php"); // Re-direct to index.php
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Mercury v1.0 | Admin System</title>
<link href="includes/global.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.mainTxt {font-size: 11px; color:#444444; font-family: Arial, Helvetica, sans;}
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #666666;
}
.logout {
	position: fixed;
	width: 50px;
	height: 25px;
	left: 801px;
	top: 57px;
	padding: 0px;
	z-index: 100;
}
* html .logout {
	position: absolute;
top:expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop : document.body.scrollTop);
	top: 57px;
	}
.register {
	position: fixed;
	width: 110px;
	height: 25px;
	left: 801px;
	top: 5px;
	padding: 0px;
	z-index: 100;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #333333;
}
* html .register {
	position: absolute;
top:expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop : document.body.scrollTop);
	top: 4px;
	}
-->
</style>
<link href="includes/admin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
	background-image: url(admin/images/yrepeat.gif);
}
-->
</style></head>


<body>
<div class="register"> To register another user to Mercury &copy; &reg;<br />
    click <a href="join_form.php">here</a> <br/>
</div>
<div class="logout">
    <form method='post' action='logout.php'>
        <input type='submit' value='logout' name='logout' />
    </form>
</div>
<?php
$image = "";
$web_image_folder = 'http://www.acmeart.co.uk/mercury/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)) {
     $image = $image_name.'.'.$ext;
   }
}

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



// 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>&nbsp;</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>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</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>&nbsp;</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>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</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

?>
<div class="fixed2"><img src="admin/images/content_header.gif" alt="content header" width="648" height="281" class="fixed2" /></div><div class="fixed1"><img src="admin/images/navbg.gif" alt="nav bg" width="152" height="600" /></div>
<div class="fixed5">
    <div id="data">
	<?php echo date('l dS F Y h:i:s A'); ?>
	    
    <p><?php printf("Mercury has successfully sent out %d broadcasts.<br />
		 <BUTTON onclick=\"window.close();\">close this window</BUTTON>
		 ", count($merc_list)); ?>
	</div>
</div>
</body>

</html>

sorry about the all the code.

i may be clutching at straws but could it be i have put the if file exists function. i will move this so it is inside the message variable and see if that changes the outcome.

thought as much that didnt work. is it possible to do what i need it to

My guess having seen the above would be that the variable $broad_img1 is out of scope... Try declaring it at the top of the script alongside $image = ""; or alternatively, reassign the HTML <img> string to the $image variable again.

R

Member Avatar for amigura

file_exists will not work on http://

$path_image_folder='mercury/image/thumbs';

foreach($exts as $ext) {
   if (file_exists($path_image_folder.'/'.$image_name.'.'.$ext)) {
     $image = $image_name.'.'.$ext;
   }
}

robothy

thanks for the reply i will try that and see if that has any effect on the outcome.

amigura

since the images will be sent out as part of an email i need the absolute path with them. so for the if file exists section if i use relative paths there that would work. when it comes to displaying them could i write something like

'http://www.acmeart.co.uk."/". $web_image_folder."/".$image . '

would that code work i will try it anyway but if it is wrong can you please show me where i went wrong.

i have got the images coming through with the email. :icon_smile: the code now looks like 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)) {
     $image = $image_name.'.'.$ext;
   }
}

// 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.
}

but i have now worked out i dont think my file unlink is working my code for this is this

$fileToRemove1 = 'image/thumbs/thumb_image1';
$exts = array('jpg', 'png', 'gif', 'jpeg');				

foreach($exts as $ext) {

   if (file_exists($fileToRemove1.'.'.$ext)) 
     {
        @chmod(0777, $fileToRemove1.'.'.$ext);
     }
     @unlink($fileToRemove1);
   }

:) remove @ and check if it throws any error. You are supposed to delete the file with the extension !

Kevin,

If you're sending the images out in an email, then yes you're right, the absolute links would be required. For the file exists, the relative links would suffice, or use the $_SERVER function (I think that is right).

A good way to test for the time being might be to produce a web page as opposed to an email. Get that working and then send it out as an email?

Just a thought.

Rob.

robothy

the page that is sent out in an email is a html page which has some dynamic elements added to it. the images are being sent now. the part which aint workin in the unlink side of it the files that are uploaded stay there unless i physically go on to the server and delete them myself.

Navman33

thank for the hint i will go and check that out now and see what is happening.

Nav33n

sorry bout the spelling mistake on your name i was just talking to someone about sat navs.

it has given me the error message Warning: chmod(): No such file or directory in /home/acmeart/public_html/mercury/upload.php on line 28 Warning: unlink(image/thumbs/thumb_image1): No such file or directory in

heh.. no probs ! As the warning itself says, chmod didn't find the file (and hence unlink didn't work). Check if the path to the file is correct for chmod and unlink!

i have edited the code slightly and am getting a different error. it is now saying

[TEX]Warning: unlink(thumb_image1): No such file or directory in /home/acmeart/public_html/mercury/upload.php on line 28[/TEX]

please correct me if i am wrong but to me it looks like it is trying to delete a file from the wrong place. the file i want removed is in the directory

[TEX]image/thumbs[/TEX]

the warning looks to me if it is lokking for the image in the [TEX]mercury/[/TEX] directory on the server.

sorry about the stupid text above i didnt think it would come out like that the warring reads Warning: unlink(thumb_image1): No such file or directory in /home/acmeart/public_html/mercury/upload.php on line 28 the directory the images are in is images/thumbs and my code now looks like

$path='image/thumbs/';
$fileToRemove1 = 'thumb_image1';
$exts = array('jpg', 'png', 'gif', 'jpeg');				

foreach($exts as $ext) {

   if (file_exists($path.'/'.$fileToRemove1.'.'.$ext)) 
     {
     unlink($fileToRemove1);
	 }
   	}

Hey Kevin,

For the unlink, your code would need to read:

$path='image/thumbs/';
$fileToRemove1 = 'thumb_image1';
$exts = array('jpg', 'png', 'gif', 'jpeg');				

foreach($exts as $ext) {

if (file_exists($path.'/'.$fileToRemove1.'.'.$ext)) 
{
unlink($path.'/'.$fileToRemove1.'.'.$ext);  // you need to provide the full path and the extension
}
}

we must have got on to that at the same time i was just about to put this up when i seen your reply. thanks. i have got the code to work now i modified it so it now looks like this

<?php
$path='image/thumbs/';
$fileToRemove1 = 'thumb_image1';
$exts = array('jpg', 'png', 'gif', 'jpeg');				

foreach($exts as $ext) {

   if (file_exists($path.'/'.$fileToRemove1.'.'.$ext)) 
     {
     unlink($path.'/'.$fileToRemove1.'.'.$ext);
	 }
   	}
?>

i have moved the code on to its own page and used the include once function and it works fine it removes the file.

the problem now is the cache it is displaying old images instead of the new ones that have been uploaded. i found this code to stop the caching of the images but it has not seemed to work

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">

Sorry Kevin, caching is something I've not used yet, as I am still new to PHP. Best of luck sorting that though.

thanks for your help robothy (sorry if spelt wrong) if you ever need any help with anythin i will do my best to help. i am not the best but i do know bits.

Member Avatar for amigura

if you are viewing the message in a email and you have just changed the image then it will always point to the old image because it has been download and stored in inbox.
the code below will only work for webpages not in email messages, but i could be wrong

<meta http-equiv=“Expires” content=“0”>
<meta http-equiv=“Pragma” content=“no-cache">
<meta http-equiv=“Cache-Control” content=“no-cache”>
<meta http-equiv=“Expires” content=“Fri, 11 May 2007 12:00:00 GMT”>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.