Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    displaying pictures from database using php

    Hi, I'm trying to use PHP to stiore and display pictures in a mysql database. At the moment the database seems to store the image without trouble, but i'm having real trouble gettig PHP to display the image in a web page. the code for storing the image is:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
     <head>
      <title>testing images</title>
     </head>
     <body>
      <?php
    	 
     require_once("C:\Server\Apache2\mysql_connect_york.inc");
    	 
     //check if form has been submitted
     if(isset($_POST['submit']))
     {
     //read uploaded file
     $image = addslashes(fread(fopen($_FILES['the_file']              ['tmp_name'], "r"),$_FILES['the_file']['size']));
    	 
     //generate the query 
     $query="insert into image_1 values(1,1,'$image','{$_FILES['the_file']['type']}')";
    	 
     //execute the query and report on success
     if(mysql_query($query))
     {
       echo'Image number'.mysql_insert_id().'has been stored!';
     }
     
     else
     {
       echo 'The image could not be stored in the database!'.mysql_error();
     } 	  
    		
     //close db connection
     //mysql_close();
    	}
    	
    	else
    	{
    	 //display the form
    	 ?>
    	 
    	 <form action ="store_binary.php" method="POST" enctype="multipart/form-data">
    	  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    	  Select a file to upload:
    		 <input type="file" name="the_file" />
    		 <br />
    		 <input type="submit" name="submit" value="Submit!" />
    	</form>
    	<?php
    	}
    	?>
    	
    	
    		</body>
    	</html>
    and the code to display the image is
    Code:
     
    <?php
    
    		 if (isset($_GET['i']))
        {
     
        //establish connection to db
    		require_once("C:\Server\Apache2\mysql_connect_york.inc");	
    		  	
    		//retrieve image info
    		$query="select image, from image_1 where image_id={$_GET['i']}";
    		
    		if($query_result=mysql_query ($query))
    		{
    		  $image=mysql_fetch_array($query_result);
    		  header("Content-type: $image[1]");
    		  echo $image[0];
    		}
    		
    		//close connection
    		mysql_close();
    		
    	}	
    	?>
    any help would be really appreciated as i've been stuck on this problem for a couple of days now!!
    Steve

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Don't upload the image into the db, it'll use up alot of resources. Simply store the image in a directory and put the filepath into the db. Should also do some security checks before querying the db straight from a variable from the url.

    Just link the php page as an image

    <img src="displayimage.php?i=1">

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for that, you're not the first person to advise storing images in a directory. However I'm struggling to find a good tutorial which walks you through the process, do you know of onw?
    Steve

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Just copy the file and move it to a directory:
    PHP Code:
    copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']); 
    That puts it into the 'files' directory

    $_FILES['imagefile']['name'] is the name of the file
    $_FILES['imagefile']['size'] is the size
    $_FILES['imagefile']['type'] is the type, ie image/gif


    So in your query to insert into the db, just put the filepath in before the filename and voila


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •