Jump to content

if file exists


kev wood

Recommended Posts

i am trying to add an image to an email.  the image it being taken from the server and it is not being displayed within the email.  if someone could please show me i am going wrong.  the code i have used is this:

 

$image = false;
$exts = array('jpg', 'png', 'gif', 'jpeg');
$image_name = thumb_image1.'.'.$exts;


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

// check if we have an image
if ($image) {
// ok we have the image
$web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs';

} else {
// error, we can't find the image.
}

 

the code i have used to display the image in the body of the email looks lie this:

 

<img src=".$web_image_folder.'/'.$image." />

 

all this code is on the same page so if the code is correct it should display the image in the email. 

Link to comment
Share on other sites

your file_exists() function is probably not finding the image since it needs a path (i.e. $web_image_folder)

 

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

 

of course you need to define the path first  :P

 

 

Also, since $image ends up containing a string, it's probably a better idea to define the 'default' as:

 

$image = "";

 

and then

 

// check if we have an image
if ($image != "") {
// ok we have the image
$web_image_folder = 'http://www.acmeart.co.uk/mercury/image/thumbs';

} else {
// error, we can't find the image.
}

 

 

... although this bit of code has now become redundant ;)

Link to comment
Share on other sites

thanks for the reply i have now changed my code and it gives the same result which is no pictures are displayed.  my code now looks like this below:

 

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


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



} else {
// error, we can't find the image.
}

 

the image that has needs to be displayed in the email gets uploaded first and is saved on the server in the folder image/thumbs/ and it is given a name on upload which is thumb_image1.

 

i have checked on the server and the file does exist,  i might be clutching at straws but could it be the way i am trying to display the image?

Link to comment
Share on other sites

Nope, it's still the way you're checking :)

 

This should work:

 

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


// 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
    echo '<img src="' . $web_image_folder.'/'.$image . '" />';
} else {
    // error, we can't find the image.
}
?>

Link to comment
Share on other sites

this has not worked either. ???

 

the files are on the server i have checked that.  could it have something to do with the array for the extension of the file.  this is the only other thing i can think it could be.  the path to the image folder where they are held is correct, the are stored on the server with the file name thumbs_image1 then the extension follows.

 

the code tells the computer to go to the correct place and look for the correct file name but it returns nothing.

 

also i dont need the image echoed inside the php code as it will be displayed further down the page so i have changed the echo to variable name and it now looks like this:

 

$broad_img1='<img src="' . $web_image_folder.'/'.$image . '" />';

 

this should not make a difference to the code working, and i tested the code before i made the change and it still was not displayed, although the echo statement was not part of the body of the email so this could have been the reason for it not displaying the first time.

 

does the img tag need to be written any differently since it is being placed into a variable.  this variable will then be placed in the body of the email and used to display the image as part of the email sent out.

 

Link to comment
Share on other sites

You could try this for debugging, I've added some echoes to output variables at each stage of setting the image:

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


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

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

You could also try setting dimensions for the image. It may not be displaying because you've got no width or height.

Link to comment
Share on other sites

hmm ... the array SEEMS okay, although i hate foreach...as loops, hehe.

 

having said that ... i may have spotted a mistake in my code i submitted:

 

$exts = array('jpg', 'png', 'gif', 'jpeg');
$image_name = 'thumb_image1.'.'.$exts';

 

we're putting an array ($exts) as a part of the string ($image_name) !!  ... maybe that's the problem?? Change $imagename to:

 

$image_name = 'thumb_image1';

 

... hopefully that helps, because you're never gonna find an image called "thumbs_image1.Array.jpg". I never spotted that in my posting, hehe. Ooops! ::)

 

To answer the img tag writing question ... no, you write as you want displayed at all times. :)

Link to comment
Share on other sites

it still not working. ???

 

could it be how i am trying to use the code.  what i meanis that the page that this code is being entered onto is the send email page which is sent from a button click.

 

what i have built works like this:

 

The user enters a title into a box and some text to go with it once they have done that they then upload an image which is stored on the server in the location mentioned earlier.  the user is then able to go to a preview page where they can see what will be sent out.  to get the images to display on this page i have used a mysql database storing the path to the image.  this section all works fine.

 

the next step if the user is happy with what they see they can click on the send button, which then sends out the email.  the title and text are sent with the images that are hard coded in, but the images which i am tryin to get from the server are not sent.

 

the page that is sent out i will put the code up here so you can see how the page works and see if you can spot any mistakes which would stop it from sending out the images from the server

<?php
$image = "";
$web_image_folder = 'http://.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://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=\"28\" colspan=\"2\" scope=\"col\"><span class=\"mainTxt\"><strong>$broad_topictitle2</strong></span></td>
    </tr>
    <tr>
        <th height=\"95\" scope=\"col\"><div align=\"left\"><img src=".$web_image_folder2.'/'.$image2." /><p> </p></div></th>
        <td 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://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";

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

// Subject
$subject = "$merc_messageTitle";

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

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

?>

Link to comment
Share on other sites

Hmm ... well i know it wouldn't like the missing www (well, more accurately it won't know how to properly interpret the single . - but i stand to be corrected!)

 

Give me some time with it, i'll pore over the code with a fine-toothed comb.

 

 

 

Trust me, seemingly insurmountable problems are always the result of a missing bloody comma :P

Link to comment
Share on other sites

Okay dude, i've got it working on my side :) I made several (minor) changes:

 

just remove the 'my stuff' with whatever hehehe

 

<?php
// ==== MY STUFF ====
$merc_messageTitle = "this is a message!";
$merc_mailingList = "david@digiworks.co.za";
// ==================


$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> </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>Test Email hahaha</p>
        <p> </p>
        <p>I\'m sooooo clever! </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 = "";
$replyEmail = $merc_list[$i];

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

// Subject
$subject = $merc_messageTitle;

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


if (mail($contactemail, $subject, $message, $headers)) {
	echo "Success!! Yay!!";
} else {
	echo "Boooooo it failed!";
}

} // END for

?>

 

You'll notice the PHP_EOL for platform-independant newlines (yay!) and i've changed the way your message is constructed ... no more escaping " :) looks neater yes?

Link to comment
Share on other sites

yes it does look neater.  did the images come through on your email from the server because they are not on mine.  i am using outlook express to view the email could it have something to do with that?  i doubt it though.

Link to comment
Share on other sites

Heya,

 

Yeah the images did come through. I use a program called PostCast Server ... google it and download it, it's free ... you can then use your 'localhost' as an SMTP server, but you can preview the messages before you actually 'send' them. I did that on my side and it found the images. outlook express might be having a fit about it, but try sending your messages to a gmail or yahoo account ... see what that does?

 

if all else fails, check my last posting under 'MY STUFF' for my email address and email me ... ;) i'll certainly let you know! hehe

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.