CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   How to display image from a database (http://www.codingforums.com/showthread.php?t=273426)

gazaian1 09-18-2012 07:53 AM

How to display image from a database
 
i have a script i wrote that uploads an image to the database then moves it to a directory leaving only image path in the database but when i try to display the image i does not show any help appreciated. Thanks

Upload script
PHP Code:

if(isset($_POST['subbutton'])){

$name $_POST['namet'];
$email $_POST['emailt'];
$about $_POST['about'];
$location $_POST['locationt'];
$website $_POST['websitet'];
$comment $_POST['commentt'];

        
$uploadDir 'wp-content/themes/speedy/testimonial/'//Image Upload Folder
        
        
$allowed_ext =  array('jpg''jpeg''png''gif');
        
        
$fileName addslashes($_FILES['pict']['name']);
        
$tmpName  $_FILES['pict']['tmp_name'];
        
$fileSize $_FILES['pict']['size'];
        
$fileType $_FILES['pict']['type'];
        
$file_ext strtolower(end(explode('.'$fileName)));
        
$filePath addslashes($uploadDir $fileName);
        
$result move_uploaded_file($tmpName$filePath);

if (
in_array($file_ext$allowed_ext) === false){

}        
if(
$name!="" && $email!="" && $comment!=""){
    
$query="INSERT INTO testimonial(id, name, email, about, location, website, picture, comment)
              VALUES ('', '$name', '$email', '$about', '$location', '$website', '$filePath', '$comment')"
;
              
mysql_query($query)  or die(mysql_error());
              echo 
"<br/><b style='color:green'>Your testimonial has been received</b>";
}else{
  echo 
"<br/><b style='color:red;'>there was an error.</b>";
}
  
$to "dianreidreid@gmail.com";
$subject "Testimonial approval request";
$name $_POST['namet'];
$email $_POST['emailt'];
$comment $_POST['commentt'];
$message "
Name: $name $email<br/>
Email: $email_field<br/>
Message: $comment"
;
$headers  'MIME-Version: 1.0' "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
// Additional headers
/*$headers .= 'To: <[email address goes here]>' . "\r\n";
$headers .= '[From] <[email address goes here]>' . "\r\n";*/
// Mail it
mail($to$subject$message$headers);    
  


This is how i try to display the image
PHP Code:

 $per_page = 4;

                        $pages_query = mysql_query("SELECT COUNT(id) FROM testimonial")or die(mysql_error());
                        $pages = ceil(mysql_result($pages_query, 0) / $per_page);

                        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        $start = ($page - 1) * $per_page;
                        
                        $query = mysql_query("SELECT name, comment, picture FROM testimonial LIMIT $start, $per_page ");
                        $count=mysql_num_rows($query);
                        
                        $pic=file_exists($query_row['picture']);
                        
                        $count=1;
                        while ($query_row = mysql_fetch_array($query)){
                        ?>
                        
                      <?php if($count==1){ ?>
                        <div class="testmon_box">
                      <?php ?>
                      <?php if($count<=1){ ?>    
                      <div class="TESMON_INNERBOX">
                       
                         
                                
                     [COLOR="Blue"] <div class="pic_block">
                            <?php if($pic !=""){?>
                                <img src="<?php echo $pic?>" />
                            <?php } else{?><img src="<?php bloginfo('template_url'); ?>/images/pic_02.jpg" /><?php ?>
                            
                      </div>[/COLOR]
                        
                       <div class="test_cont_block">

                          <p> <?php echo $result=$query_row['comment']; ?></p>

                         <span class="danam"> 
                        Date of Posting: <?php echo date('l, F j, Y'); ?><br/>

                        <strong>Posted by: <?php echo $result=$query_row['name']; ?></strong>

                         </span>
                         
                       </div>
                            
                             
                             
                       <div class="clear"> </div>

                        </div>

                    

                        </div>
                       <?php ?>
                              
                       <?php ?>
                     
                      <div class="pagin_nav">
                         <?php if ($pages >=&& $page <= $pages){
                           for (
$x=1$x<=$pages$x++){
                                echo (
$x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></srtong> ' '<a href="?page='.$x.'">'.$x.'</a> ';
                              
                           }
                         }
?>


vnbenny88 09-18-2012 10:55 AM

Please tell me, what path is saved in your database?

tangoforce 09-18-2012 12:43 PM

I just want to clarify something here.. from this code you're storing the image path to a file on the disk yet in your previous topic you state that you are using a blob and can see the blob size? :confused: Which is it and did you amend the data if you've restructured your columns? - I only ask because if picture is still containing blobs and you're rying to print file paths..

gazaian1 09-18-2012 04:47 PM

Quote:

Originally Posted by tangoforce (Post 1270924)
I just want to clarify something here.. from this code you're storing the image path to a file on the disk yet in your previous topic you state that you are using a blob and can see the blob size? :confused: Which is it and did you amend the data if you've restructured your columns? - I only ask because if picture is still containing blobs and you're rying to print file paths..


Sorry i am just a bit confused i only see BLOB and a size in the database. Does that mean their is nothing stored.

tangoforce 09-18-2012 05:16 PM

Right so if you're seeing a blob then you have a field that is taking blob data.

What are you putting into it - from the code you've shown above, it looks like you're storing the filename and path yet from the code in your previous topic you were storing the file itself in the blob field. Which is it?

gazaian1 09-18-2012 05:27 PM

Quote:

Originally Posted by tangoforce (Post 1270989)
Right so if you're seeing a blob then you have a field that is taking blob data.

What are you putting into it - from the code you've shown above, it looks like you're storing the filename and path yet from the code in your previous topic you were storing the file itself in the blob field. Which is it?

Yes i want to store the file path in the database which is link to a directory on my server. The images are stored on the server when uploaded but i can't get it to display on the front end. How can i achieve this please also how can i my the upload field not required.

tangoforce 09-18-2012 06:10 PM

Ok so you need to test the file path to the image then. You're not far from it, you've got it printing the file path into your html so now you just need to figure out whats wrong with the final path in the html.

You can copy it out and add it onto the url in the browser bar - thats one way of testing it.

gazaian1 09-18-2012 06:36 PM

Quote:

Originally Posted by tangoforce (Post 1271008)
Ok so you need to test the file path to the image then. You're not far from it, you've got it printing the file path into your html so now you just need to figure out whats wrong with the final path in the html.

You can copy it out and add it onto the url in the browser bar - thats one way of testing it.


Thanks for your help so far here is a link to my site:
http://speedycarloans.ca/testimonials/

Right now it only display the default image because i used an if statement to check if the database is empty or not.

tangoforce 09-18-2012 07:15 PM

Quote:

Originally Posted by gazaian1 (Post 1271021)
Right now it only display the default image because i used an if statement to check if the database is empty or not.

Well that won't really help much. Unless you print the path to the image in the html, how do you expect to test it?

Len Whistler 09-18-2012 08:21 PM

I wouldn't store the path to the image in the database, just the file name - with or without the ext. Ex: ny-city-9456.jpg or ny-city-9456

The image path should be in the while loop that queries the database.



----

gazaian1 09-18-2012 09:05 PM

Quote:

Originally Posted by Len Whistler (Post 1271055)
I wouldn't store the path to the image in the database, just the file name - with or without the ext. Ex: ny-city-9456.jpg or ny-city-9456

The image path should be in the while loop that queries the database.



----

Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]

Len Whistler 09-18-2012 09:13 PM

Quote:

Originally Posted by gazaian1 (Post 1271072)
Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]


The MySQL data type should be something like VARCHAR or TINYTEXT maybe 30 characters for an image file name. You don't need BLOB.





---

tangoforce 09-18-2012 09:15 PM

Quote:

Originally Posted by gazaian1 (Post 1271072)
Ok when i try to store the file path or name i only see this in the database: [BLOB - 59B]

As I asked you earlier, did you restructure the database (obviously no) and did you change the contents from the file itself to the filename and path instead? - Clearly this looks like no too.

You posted another topic today. In that code you were putting the actual file inside the database. Since then you've decided to use the filename / path.

When you change your mind and decide to use the filename instead of the file stream, you must delete the old files that are stored in the database and change the column from a blob to a text field instead.

I suspect the reason none of your images are showing is because instead of printing a file name and path in your html, you're trying to print a filestream instead.

gazaian1 09-18-2012 09:36 PM

Quote:

Originally Posted by tangoforce (Post 1271077)
As I asked you earlier, did you restructure the database (obviously no) and did you change the contents from the file itself to the filename and path instead? - Clearly this looks like no too.

You posted another topic today. In that code you were putting the actual file inside the database. Since then you've decided to use the filename / path.

When you change your mind and decide to use the filename instead of the file stream, you must delete the old files that are stored in the database and change the column from a blob to a text field instead.

I suspect the reason none of your images are showing is because instead of printing a file name and path in your html, you're trying to print a filestream instead.

Sorry i am kind of new to this.

i changed the type to varchar with a length of 255 characters but till no image.
this is what i have for my output code now
PHP Code:

    <?php   
                        $per_page 
4;

                        
$pages_query mysql_query("SELECT COUNT(id) FROM testimonial")or die(mysql_error());
                        
$pages ceil(mysql_result($pages_query0) / $per_page);

                        
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        
$start = ($page 1) * $per_page;
                    
                        
$query mysql_query("SELECT id, name, comment, picture FROM testimonial LIMIT $start, $per_page") or die(mysql_error());
                        
$count=mysql_num_rows($query);
                        
                        
                        
                        
                        
$count=1;
                        while (
$query_row mysql_fetch_array($query)){
                        
$pic=$query_row['picture'];
                        
?>
                        
                      <?php if($count==1){ ?>
                        <div class="testmon_box">
                      <?php ?>
                      <?php if($count<=1){ ?>    
                      <div class="TESMON_INNERBOX">
                       
                         
                                
                      <div class="pic_block">
                            <?php if($pic !=""){?>
                                <img src="<?php echo $pic?>" />
                            <?php } else{?><img src="<?php bloginfo('template_url'); ?>/images/pic_02.jpg" /><?php ?>
                            
                      </div>
                        
                       <div class="test_cont_block">

                          <p> <?php echo $result=$query_row['comment']; ?></p>

                         <span class="danam"> 
                        Date of Posting: <?php echo date('l, F j, Y'); ?><br/>

                        <strong>Posted by: <?php echo $result=$query_row['name']; ?></strong>

                         </span>
                         
                       </div>
                            
                             
                             
                       <div class="clear"> </div>

                        </div>

                    

                        </div>
                       <?php ?>
                              
                       <?php ?>
                     
                      <div class="pagin_nav">
                         <?php if ($pages >=&& $page <= $pages){
                           for (
$x=1$x<=$pages$x++){
                                echo (
$x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></srtong> ' '<a href="?page='.$x.'">'.$x.'</a> ';
                              
                           }
                         }
?>


Len Whistler 09-18-2012 09:43 PM

Quote:

Originally Posted by gazaian1 (Post 1271084)
Sorry i am kind of new to this.

i changed the type to varchar with a length of 255 characters but till no image.
this is what i have for my output code now

A good way to debug PHP is to View Page Source and see what is missing in the HTML. I use FireFox to view the page source, I don't know about IE or Opera.



---


All times are GMT +1. The time now is 10:54 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.