Jump to content

[SOLVED] displaying images using the absolute path


kev wood

Recommended Posts

i was wondering if it is possible to display an image in a email from a database.  i no that images can be displayed in a email using the absolute path to the image but can the same be done if the absolute path for the directory is used for where the images are stored in the database on the server.

 

i have used some php code to display the pth to the directory which it gives but can this then be used to display the image in the email?

 

the code i used to get the path looks like this:-

 

<?php
$query = "SELECT broad1 FROM images_broad";
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<img src="'.$row['broad1'].'"/>';
}

$path = getcwd();
echo "The absolute path to this directory is: ";
echo $path;

					      
?>

Link to comment
Share on other sites

when the images are stored in the db the are stored using the path to the image, if i use the absolute path to the image would they then display within the email which will be a newsletter.

 

the images that i want to be displayed will have only just been uploaded on to the server and are stored in a mysql db on the server.  i have tried a few different ways of getting the images to display in the email with no joy yet.

 

on looking into it i think it is because when the images are trying to be displayed the only have a relative path stored in the db so they would only ever display if the user was actually on the site itself and not opening an email.

Link to comment
Share on other sites

on looking into it i think it is because when the images are trying to be displayed the only have a relative path stored in the db so they would only ever display if the user was actually on the site itself and not opening an email.

 

You need a serve script that (obviously) has access to this path, and then streams binary data...

Link to comment
Share on other sites

so if i set the access to the db at the top of the page and stored the absolute paths to the images in the db then they should display in the email?

 

think that is what you meant.

 

or would i have to use http requests to get the data from the database and stream it to the email?

 

was not to sure what you meant, sorry.

Link to comment
Share on other sites

so if i set the access to the db at the top of the page and stored the absolute paths to the images in the db then they should display in the email?

 

think that is what you meant.

 

or would i have to use http requests to get the data from the database and stream it to the email?

 

was not to sure what you meant, sorry.

Well, if you're trying to show an image in an HTML e-mail, then you need to use a SRC tag somewhere, which means you have to issue a request to this image with a URI, which means it likely starts with http.

Link to comment
Share on other sites

the email that is dynamically created from user inputs so the page that is emailed out is a php page.  the images on the page are uploaded by the user so the img tag would not work for this as the images are taken from a mysql db.  if the absolute path was stored in the db and the php page had the db connection details at the top of the page would i be able to display the images this way.

 

some of the images on the page are hard coded in so the i have been able to display these on the page but that is the only images that display.

 

sorry if i is being a pain.

Link to comment
Share on other sites

What do you mean "so the img tag would not work"?  You're using PHP to write out HTML code.  That's the only way to display an image in HTML.

 

I think you're confusing your e-mail page with your image serve script.

Link to comment
Share on other sites

the email is going to be a newsletter.  the newsletter is created by the user, first of all the banner at the top has been created in photoshop so this has been hard coded in using the img tag.  the next part of the newsletter is the text, this is entered by the user at the time the newsletter (nl) is being created.  the text appears on the nl by the user entering in a title into a text field and then the body text into a text field.  the user is then asked to upload an image.  once those steps have been completed they can then preview this page.

 

the preview page works fine all text and images show up.  if the nl is how they want then they can hit the send button which then sends the email out to the mailing list.

 

once the images are uploaded they are first stored on the server then the path to these images is stored in a mysql db.  the preview page works by the title and the text are stored in variables and they are posted to the preview page, the images are retrieved from the db and displayed on this page.

 

once the email has been sent as the images are held on the server and only the relative path is stored in the db they do not show up on the nl but all the text comes through.  so the email only consists of text with no images.

 

the code used to display the images on the preview page looks like this

 

<?php

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

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

 

what i need to know is if i can store the absolute path to these images so they can be accessed when the email is opened.  or if this cannot be done then how can i display these uploaded images in the email that has been sent out.

 

sorry if i am not clear on what i mean but if you need me to post anymore of the code please ask.  i have been working on this for a while now and this is the last section i need to get fixed and then i can forget about trying to get it to work and start sending out nl to my mailing list.

 

i know it is possible to send out just html as an email but i need to be able to send html with some php/mysql.

 

 

Link to comment
Share on other sites

<?php

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

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

 

what i need to know is if i can store the absolute path to these images so they can be accessed when the email is opened.  or if this cannot be done then how can i display these uploaded images in the email that has been sent out.

 

sorry if i am not clear on what i mean but if you need me to post anymore of the code please ask.  i have been working on this for a while now and this is the last section i need to get fixed and then i can forget about trying to get it to work and start sending out nl to my mailing list.

 

i know it is possible to send out just html as an email but i need to be able to send html with some php/mysql.

You're obviously not understanding what I'm trying to say.  You are storing you images somewhere on your server, in a private (non world readable) directory.  So simply dumping out the path into an SRC tag won't do anything, since the browser will have no way to reach it.  I will say it again -- you need to write a SEPARATE image serve script that has access to this directory, and simply produces a binary stream of data with the appropriate HTTP headers.  If you don't know how to do this, I'm sure there are plenty of tutorials out there -- I'm not versed in PHP.

 

And to the e-mail client, it's STILL just HTML -- forget about the fact that php is generating it and that it's pulling stuff from a back end database.  If the HTML page that you produce with PHP doesn't have proper SRC tags, it won't work.

 

Your code probably needs to look like:

 

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

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<img src="http://www.your-domain-name.com/foo/bar/image_serve.php?broad_id='.$row['broad_id'].'"/>';
}

 

And then simply have that script take in the ID of that record, look up the path, binary read in the file, and then output a valid http image.

 

Does that make more sense?  If not, I can't think of any other ways to explain it... maybe someone else has another way.

Link to comment
Share on other sites

thanks for the reply it does make some sense but i still do have some gray areas.  did you mean that the code i have used on the preview page will not work or that it will not work for the email.  as this code does display the image on that page.

 

if i use the line of code below with the image_serve.php page should this display the images in the email.

 

echo '<img src="http://www.your-domain-name.com/foo/bar/image_serve.php?broad_id='.$row['broad_id'].'"/>';

 

Link to comment
Share on other sites

did you mean that the code i have used on the preview page will not work or that it will not work for the email.  as this code does display the image on that page.

Then perhaps I don't understand what's in broad1...

Link to comment
Share on other sites

sorry i took so long to get back i have scrapped the mysql side and i am now using php to save the files in a directory then using the absolute path to display the images in the email.  just got the problem of how to display the image if the file extension is different to the one that has been up loaded.

 

what i mean by this is the img src tag ends with .gif but if the image is a jpeg then the image will not show on the email so i am working on some code so that it will display images with jpg png of gif file types.  hopefully i can work this out because to write php image convert will take a while.

Link to comment
Share on other sites

what i mean by this is the img src tag ends with .gif but if the image is a jpeg then the image will not show on the email so i am working on some code so that it will display images with jpg png of gif file types.

That's impossible... is it a valid JPEG?  Could you show me some sample data from the table?

Link to comment
Share on other sites

  • 2 weeks later...

sorry about the no reply i was working on other solutions at the same time and i have nearly got round the problem using php if file exists.  i am displaying the images from the server now as it has less security risks than trying to display them from a mysql database.

 

thanks for all your help though.

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.