PHP picture upload script

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

<!DOCTYPE>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>

<body>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>” method=”post” enctype=”multipart/form-data”>
<h1>Upload image</h1>
<P>Select an image: </P><input type=”file” name=”s” /><input type=”submit” value=”Upload” />
</form>
<?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<br/>
Filepath : $filepath<br/>
Size : $filesize KB<br/>
Type : $filetype<br/>”;
if($filetype=”image/jpeg” or $filetype=”image/png” or $filetype=”image/gif”)
{
copy($filepath,”$upload/$filename”);
echo”<img src=\”$upload/$filename\” height=\”500\” width=\”500\”/>”;
}else{
echo”Couldn’t copy.”;
}

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

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.

Leave a comment