I am creating a social website.There is provision for storing images during registration of users.I want to display the preview of the image when the user uploads the image,before the form get submitted.Please help me.
This is my code in registration section
<form action="insert_scene.php"
method="post" id="sceneform" enctype="multipart/form-data">
<label for="image">Scene Flyer:</label>
<img src="" width="120" height="130" alt="Scene Pic" />
<a href="" onclick="return uploadimg()" > Upload</a></li>
<input type="file" name="file" id="file" style="display:none;" />
</form>
The javascript content is
function uploadimg()
{
var uploader = document.getElementById('file');
uploader.click();
return false;
}
This is the segment of code in the insert_scene.php
(Actually the following segment of code is placed in another file which is being included in the insert_scene.php
)
<?php
ini_set('memory_limit','128M');
ini_set("post_max_size", "10M");
ini_set("upload_max_filesize", "10M");
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
if ($_FILES["file"]["error"] > 0)
{
$fileN = '';
echo 'File Size:' .($_FILES["file"]["size"] / 1024);
}
else
{
$maxImgSize=($_FILES["file"]["size"] / 1024);
if($maxImgSize>=5121 || $maxImgSize==0){
$fileN = '';
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
$fileN="upload/" . $_FILES["file"]["name"];
while(file_exists($fileN)){
$fileN="upload/" . md5($fileN).$_FILES["file"]["name"];
}
rename("upload/" . $_FILES["file"]["name"], $fileN);
}
}
?>
$scene_flyer = $fileN;
After posting the form,$scene_flyer
gets the image address.But i would like to display the preview before the form in the first page gets submitted.Please help me.