0

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>";
 }

 ?>
2
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by niths
0

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.

Edited by vibhaJ: n/a

0
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

0
<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".

0

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?

0

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.

Edited by niths: n/a

0

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

0

i removed "$con" from

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

line 62. and now i got it.

Edited by niths: n/a

0

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

0

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>

Edited by vibhaJ: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.