How do I display user uploaded images?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

How do I display user uploaded images?

Post by ninethousandfeet »

hello,

i'm very new to php and i have no idea where to start on this problem. i've finally figured out how to properly allow user to upload image files. the files are stored in my directory and then the following are sent to the db: post_id, post_title, product, user_id, datetime, image_name, image_type.
up to now, this works.
now what? let's say for example John Q Sample logs in, does a search for a product, and notices a listing that interests him. John click the link of this product description and is taken to a page that displays the product info and also the image. How do i make this work? i've read some info about the fopen(), etc. but i feel like i'm missing some key steps. i don't know where the beg., middle, and end go or in what order.

any help and/or guidance? thank you for reading this.
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: How do I display user uploaded images?

Post by tech603 »

db: post_id, post_title, product, user_id, datetime, image_name, image_type.
You pretty much have what you need here. When the user clicks on the product just do a query on the database for the product, description and image ect..

This tutorial may help you get on the right path.

http://www.w3schools.com/PHP/php_mysql_select.asp
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: How do I display user uploaded images?

Post by ninethousandfeet »

i'm okay with displaying the info from the db, but that doesn't allow me to get the image from my directory. all that goes to the db in regards to the image is picexample.gif, whereas the directory receives the actually image and it's stored in home/mysite.com/public_html/upload/$username/$file.

my question is how do i get the image to appear on a page when the image is stored in the directory? all i get if i do a query of the db is the picexample.gif in text on the user's browser, i want to display the image.

any suggestions?
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: How do I display user uploaded images?

Post by tech603 »

Because you store the image name in the database it makes it easy to find it. You already know where the folder is where you store your images and in your database you have the name.

Code: Select all

 
//your directory variable
$folder = "/yourdirectory/images/";
 
$result = mysql_query("Select productName, productInfo, productImage from productTable");
while(list($name, $info, $image) = mysql_fetch_array($result))
{
echo '<tr><th>Product Name</th><th>Product Description</th><th>Image</th><tr>
        <tr><td>' .  $name . '</td><td>' . $info . '</td><td><img src="' . $folder . $image . '</td></tr>';
}
 
Something like that.
Hope that helps.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: How do I display user uploaded images?

Post by ninethousandfeet »

hey,

so, a couple of problems i'm having:
1) every time i try to use <img src i get this error:
Parse error: syntax error, unexpected '<' in /home/me/domains/mysite.com/public_html/TESTviewIMAGE.php on line 101
2) when users upload the images, the file name on their computer goes to the db, but the file then goes to my directory with some additional subfolders if for example the file already exists then the new one embeds itself further... (so a user uploading pic.gif for the first time would have pic.gif sent to the db under the field 'image_data'... and then the image would go to my directory: /home/me/domains/mysite.com/public_html/upload/$username... if they were to enter pic.gif again it would go to /home/me/domains/mysite.com/public_html/upload/$username/$sub... and so on...
here is the code with your recommendations and i get the error above... what am i doing wrong? what's the trick to the img src tag?
thank you!

Code: Select all

 
$username = $_SESSION['MM_Username'];
$folder = "/home/me/domains/mysite.com/public_html/upload/$username";
 
$var1_getUser = "-1";
if (isset($_SESSION['MM_Username'])) {
  $var1_getUser = $_SESSION['MM_Username'];
}
mysql_select_db($database_connUser, $connUser);
$query_getUser = sprintf("SELECT userTable.user_id, userTable.username FROM userTable WHERE userTable.username = %s", GetSQLValueString($var1_getUser, "text"));
$getUser = mysql_query($query_getUser, $connUser) or die(mysql_error());
$row_getUser = mysql_fetch_assoc($getUser);
$totalRows_getUser = mysql_num_rows($getUser);
 
$var2_getPost = "-1";
if (isset($_GET['post_id'])) {
  $var2_getPost = $_GET['post_id'];
}
mysql_select_db($database_connUser, $connUser);
$query_getPost = sprintf("SELECT postingTable.post_id, postingTable.post_title, postingTable.product_name, postingTable.user_id, postingTable.post_date, postingTable.buy_or_share, postingTable.category_name, postingTable.image_data, postingTable.image_type, userTable.user_id, userTable.username FROM postingTable INNER JOIN userTable ON postingTable.user_id = userTable.user_id WHERE postingTable.post_id = %s", GetSQLValueString($var2_getPost, "int"));
$getPost = mysql_query($query_getPost, $connUser) or die(mysql_error());
$row_getPost = mysql_fetch_assoc($getPost);
$totalRows_getPost = mysql_num_rows($getPost);
while ($row_getPost = mysql_fetch_assoc($getPost)) {
    echo <img src" ' . $folder . $row_getPost['image_data']. '";
}
?>
 
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: How do I display user uploaded images?

Post by ninethousandfeet »

nevermind... i got it, thank you for your help!
it worked with:
<img src = "http://www.mysite.com/upload/<?php echo $row_getPost['username']?>/<?php echo $row_getPost['image_data']?>" />
Post Reply