Search code examples
phpimageuploading

image uploading and displaying


I am new for php. I want to store a selected image in a folder (name:uploads) and when the upload button is clicked i want to display the image on the same page. I tried with the below code, but i am getting error on echo statement.

<html>
<head>
<title>PHP File Upload example</title>
</head>
<body>

<form action="new.php" enctype="multipart/form-data" method="post">
Select image :
<input type="file" name="file"><br/>
<input type="submit" value="Upload" name="Submit1"> <br/>
</form>
<?php
if(isset($_POST['Submit1']))
{  
    $filepath = "uploads/" . $_FILES["file"]["name"];
    echo $filepath;

    if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) 
    {
        echo "<img src=".$filepath." height=200 width=300 />";
    } 
    else 
    {
        echo "Error !!";
    }
} 
?>

</body>
</html>

Solution

  • In this line

    <form action="new.php" enctype="multipart/form-data" method="post">
    

    change to

    <form action="" enctype="multipart/form-data" method="post"> 
    

    and also make sure that you have uploads folder (with write permission) where this php script is placed