PHP picture upload script

It is a very easy and simple PHP picture uploading script.I am giving the source script below

Untitled Document

<form action=”” method=”post” enctype=”multipart/form-data”>
Upload image
Select an image:
<?php
$upload=”img”;
if(isset($_FILES[‘s’]))
{
$filename=$_FILES[‘s’][‘name’];


$filepath=$_FILES[‘s’][‘tmp_name’];
$filetype=$_FILES[‘s’][‘type’];
$filesize=substr($_FILES[‘s’][‘size’]/1024,0,5);
print”Filename : $filename
Filepath : $filepath
Size : $filesize KB
Type : $filetype
”;
if($filetype=”image/jpeg” or $filetype=”image/png” or $filetype=”image/gif”)
{
copy($filepath,”$upload/$filename”);
echo””;
}else{
echo”Couldn’t copy.”;
}

if(copy($filepath,”$upload/$filename”))
{
echo”
Picture uploaded.
and search it by
”;
echo “localhost/teach/$upload/$filename”;
}else{
echo”No activity.”;
}
}
?>

Explanation:

1)At first create a folder in the xampp/htdocs directory.Then inside the folder create another folder named ‘img’.It is the directory where the picture will be stored.

2)Now copy the PHP script and paste it in the directory where the ‘img’ folder is located but don’t paste the PHP script inside the ‘img’ directory.

3)In this script there is variable declared name ‘$filetype‘.If the picture format is JPEG,PNG or GIF then the picture will be uploaded otherwise the script won’t work.

Advertisements