Jump to content
lauralee

viewing uploaded images

Recommended Posts

I have been using the SitePoint Book, "Database Driven Web Site" to learn PHP. I have developed several successful websites using the information I've learned. However, I am now on Chapter 12 of the 4th edition, and cannot figure out how to use the images that are uploaded on my web pages with the technique described in the book. In all my websites, I use cpanel to create a folder, images, and uploade the images into that folder. Then I write the <a href .. code that allows the image to be viewed on the web page. The code below allows uploading, downloading, and deleting image files, but in order to view them, I must click on the name of the file. I would like to be able to view the jpgs on other pages. When I try to echo the filedata as an image, I get gobbledygook. Can someone give me a hint on how to work this out? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Image File Repository</title><meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /></head><body><h1>Image File Repository</h1><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <p><label>Upload File:<br /> <input type="file" name="upload" /></label></p> <p><label>File Description:<br /> <input type="text" name="desc" maxlength="255" /></label></p> <p><input type="submit" value="Upload" /></p></form><p>The following image files are stored in the database:</p><table><tr> <th>Filename</th> <th>Type</th> <th>Description</th></tr><?phpif (mysql_num_rows($filelist) > 0) { while ($f = mysql_fetch_array($filelist)) { ?><tr valign="top"> <td> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>"> <?php echo $f['filename']; ?></a> </td> <td><?php echo $f['mimetype']; ?></td> <td><?php echo $f['description']; ?></td> <td> [<a href=<?php echo $_SERVER['PHP_SELF]; ?>?action=dnld&id=<?php echo $f['id']; ?>" >Download</a> | <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=del&id=<?php echo $f['id']; ?>" onclick="return confirm('Delete this file?');" >Delete</a>] </td></tr> <?php }} else { ?> <tr><td colspan="3">No Files!</td></tr> <?php}?></table></body></html>

Share this post


Link to post
Share on other sites

what does

<?php echo $f['filename']; ?>

output? Does it need to be in an image tag?

echo '<img src="' . $f['filename'] . '">';

Share this post


Link to post
Share on other sites

I've added the last line below: <td><?php echo $f['mimetype']; ?></td> <td><?php echo $f['description']; ?></td> <td><?php echo '<img src="/filestore/' . $f['filename'] . '" alt="image"></img>'; ?></td>Now, the alt text shows up, but still no image. In the table filestore.sql the field names are: id, filename, mimetype, description, filedata. The entries are: 1, golflogo, image/jpg, golf_logo, [bLOB 8.2 KiB].How do I tell the img tag to show the image?

Share this post


Link to post
Share on other sites
I've added the last line below: <td><?php echo $f['mimetype']; ?></td> <td><?php echo $f['description']; ?></td> <td><?php echo '<img src="/filestore/' . $f['filename'] . '" alt="image"></img>'; ?></td>Now, the alt text shows up, but still no image. In the table filestore.sql the field names are: id, filename, mimetype, description, filedata. The entries are: 1, golflogo, image/jpg, golf_logo, [bLOB 8.2 KiB].How do I tell the img tag to show the image?
well, the answer is pretty clear. Filename is just golflogo. Without the file extension, it will never display as an image. It might be a good idea when the file is inserted into the database to append the file extension to the filename, so that it's saved in the field as golflogo.jpg. This could be easily done with some string functions, extracting everything from the mime-type after the forward slash.

Share this post


Link to post
Share on other sites

Actually, the filename does have the .jpg extension. I just didn't type it in correctly in my previous response. So, I'm still stuck on this one.

Share this post


Link to post
Share on other sites

I think the <img> element should be self closing, like:

<td><?php echo '<img src="/filestore/' . $f['filename'] . '" alt="image" />'; ?></td>

not sure if that would cause the problem though. if the image just isn't displaying, you should check the path to the image, either by right clicking the image and selecting properties, or looking at the <img> element in the page's source code.maybe there's an extra folder name that shouldn't be there, meaning a '../' (back one folder) might be needed.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...