hi all,
i am having a page where i can upload images.so now my problem is i hav to view the image that is uploaded and that should display on the web page.

My code is uploading an image in the images folder. so now i need to display that image below browse button.
thank u..

<html>
<body>
  <form name="newad" method="post" enctype="multipart/form-data"  action="test.php">
 <table>
     <tr><td><input type="file" name="image"></td></tr>
     <tr><td><input name="Submit" type="submit" value="View image"></td></tr>
 </table>    
 </form>
</body>
</html>               
<?php
 define ("MAX_SIZE","1000"); 
 function getExtension($str) 
 {
         $i = strrpos($str,".");
         if (!$i) 
         { 
             return ""; 
         }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
 if(isset($_POST['Submit'])) 
 {
     $image=$_FILES['image']['name'];
     if ($image) 
     {
         $filename = stripslashes($_FILES['image']['name']);
         $extension = getExtension($filename);
         $extension = strtolower($extension);
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
         {
             echo '<h1>Unknown extension!</h1>';
             $errors=1;
         }
         else
         {
            $size=filesize($_FILES['image']['tmp_name']);
            if ($size > MAX_SIZE*1024)
            {
                echo '<h1>You have exceeded the size limit!</h1>';
                $errors=1;
            }
            $image_name=time().'.'.$extension;
            $newname="images/".$image_name;
            $copied = copy($_FILES['image']['tmp_name'], $newname);
            if (!$copied) 
            {
                echo '<h1>Copy unsuccessfull!</h1>';
                $errors=1;
            }
         }
     }
 }
 if(isset($_POST['Submit']) && !$errors) 
 {
     echo "<h1>File Uploaded Successfully! Try again!</h1>";
 }

 ?>

Recommended Answers

All 11 Replies

For displaying it back you should use mysql db.

So once image is uploaded on server i.e. @ echo "<h1>File Uploaded Successfully! Try again!</h1>";
$newname will be saved in table.

After that using select query that $newname will be fetched and shown in image src tag.

include 'connection.php';
     $sql="INSERT into images(name,size,path,comments) VALUES ('$filename','$s','$newname','data is not present')";
     if(!mysql_query($sql,$con))
     {
        die('Error:' . mysql_error());
     }

i am getting the output as 'Error' and the data is not inserted in to the database. so can anyone please

where did you place this code? rather post complete page.
What error you are getting??

<html>
<body>
  <form name="newad" action="test.php" method="POST" enctype="multipart/form-data">
    <p>Upload Image: <input type="file" name="image"><br>
    <font size="1">Click browse to upload a local file</font><br>
    <br>
    <input type="submit" name="Submit" value="View Image">
    </form>     
</body>
</html>               
<?php
include 'connection.php';
 define ("MAX_SIZE","1000"); 
 function getExtension($str) 
 {
         $i = strrpos($str,".");
         if (!$i) 
         { 
             return ""; 
         }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
 if(isset($_POST['Submit'])) 
 {
     $image=$_FILES['image']['name'];
     if ($image) 
     {
         $filename = stripslashes($_FILES['image']['name']);
         $extension = getExtension($filename);
         $extension = strtolower($extension);
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
         {
             echo '<h1>Unknown extension!</h1>';
             $errors=1;
         }
         else
         {
            $size=filesize($_FILES['image']['tmp_name']);
            $s=round($size/1024);
            if ($size > MAX_SIZE*1024)
            {
                echo '<h1>You have exceeded the size limit!</h1>';
                $errors=1;
            }
            $image_name=$filename;
            $newname="images/".$image_name;
            $copied = copy($_FILES['image']['tmp_name'], $newname);
            if (!$copied) 
            {
                echo '<h1>Copy unsuccessfull!</h1>';
                $errors=1;
            }
         }
     }
    // echo $filename;
    //  echo $s;  
    //  echo $newname;   
     $sql="INSERT INTO images(name,size,path,comments) VALUES ('$filename','$s','$newname','data is not present')";
     $data=mysql_query($sql,$con);
     if(!$data)
     {
        die('Could not enter data:' . mysql_error());
     }
 }
 if(isset($_POST['Submit']) && !$errors) 
 {
     echo "<h1>File Uploaded Successfully! Try again!</h1>";
 }
  
 ?>

i am getting error as
"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Photoediting\test.php on line 62".

At line no 61:
echo $sql="INSERT INTO images(name,size,path,comments) VALUES ('$filename','$s','$newname','data is not present')"; exit;
Check this echo query in sql window. Does it works successfully?

i had done that i am not getting any error the query is correct.even i had done like this also

echo $sql;

and in the browser when i uploaded any image i am getting that name, size and path correctly.but the only thing is they are not inserting into data base.

then there is missing with your $con. Execute any simple insert query just for testing connection and check that insert works or not?

i removed "$con" from

$data=mysql_query($sql,$con);

line 62. and now i got it.

now how can i display the uploaded image below the browser button..

lets this page is index.php.

<?
 include 'connection.php';
 define ("MAX_SIZE","1000"); 
 function getExtension($str) 
 {
         $i = strrpos($str,".");
         if (!$i) 
         { 
             return ""; 
         }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
 if(isset($_POST['Submit'])) 
 {
     $image=$_FILES['image']['name'];
     if ($image) 
     {
         $filename = stripslashes($_FILES['image']['name']);
         $extension = getExtension($filename);
         $extension = strtolower($extension);
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
         {
             echo '<h1>Unknown extension!</h1>';
             $errors=1;
         }
         else
         {
            $size=filesize($_FILES['image']['tmp_name']);
            $s=round($size/1024);
            if ($size > MAX_SIZE*1024)
            {
                echo '<h1>You have exceeded the size limit!</h1>';
                $errors=1;
            }
            $image_name=$filename;
            $newname="images/".$image_name;
            $copied = copy($_FILES['image']['tmp_name'], $newname);
            if (!$copied) 
            {
                echo '<h1>Copy unsuccessfull!</h1>';
                $errors=1;
            }
         }
     }    
     if(!$data)
     {
        die('Could not enter data:' . mysql_error());
     }
 }
 if(isset($_POST['Submit']) && !$errors) 
 {
     echo "<h1>File Uploaded Successfully! Try again!</h1>";
	 $sql="INSERT INTO images(id,name,size,path,comments) VALUES (null,'$filename','$s','$newname','data is not present')";
     $data=mysql_query($sql);
	 $LastId = mysql_insert_id();
	 header("Location:index.php?id=".$LastId);
	 exit;
 }
 if(isset($_GET['id']))
 {
 	$q= " select * from images where id=".$_GET['id'];
	$rs = mysql_query($q);
	$sar = mysql_fetch_array($rs);
	$imgPath = $sar['path'];
 }
  
 ?>
 <html>
<body>
  <form name="newad" action="test.php" method="POST" enctype="multipart/form-data">
    <p>Upload Image: <input type="file" name="image"><br>
    <font size="1">Click browse to upload a local file</font><br>
    <br>
    <input type="submit" name="Submit" value="View Image">	
	<? if($imgPath!=''){?><img src="<?=$imgPath;?>"><? } ?>
    </form>     
</body>
</html>

i should get all the images present in the database. so can we get by that code..?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.