Simple image uploading script in PHP.
First; create a file, and copy-paste above code into this file. After your form is fine, create a new file that named upload.php this file contains our main uploading action. When you are done, create a folder where we store images. That is all, now you can upload images.
<!-- Form Area -->
<form enctype= "multipart/form-data" action= "upload.php" method= "post" >
Select Image: <input type= "file" name= "userfile" >
<input type= "submit" value= "Upload!" >
</form>
<!-- Form Area -->
<?php
# Variables
$path = "images/" ;
$max_size = "200000" ;
# File
$filename = $_POST [ 'userfile' ] ;
# Control
if ( !isset ( $HTTP_POST_FILES [ 'userfile' ] ) ) exit;
if ( is_uploaded_file ( $HTTP_POST_FILES [ 'userfile' ] [ 'tmp_name' ] ) ) {
if ( $HTTP_POST_FILES [ 'userfile' ] [ 'size' ] >$max_size ) {
echo "The File is Too Big. The Max File Size is $max_size KB<br>n" ;
exit;
}
# Type Control
if (
( $HTTP_POST_FILES [ 'userfile' ] [ 'type' ] == "image/gif" ) ||
( $HTTP_POST_FILES [ 'userfile' ] [ 'type' ] == "image/jpg" ) ||
( $HTTP_POST_FILES [ 'userfile' ] [ 'type' ] == "image/bmp" ) ||
( $HTTP_POST_FILES [ 'userfile' ] [ 'type' ] == "image/png" ) ||
( $HTTP_POST_FILES [ 'userfile' ] [ 'type' ] == "image/jpeg" )
)
{
# If File Exist
if ( file_exists ( $path . $HTTP_POST_FILES [ 'userfile' ] [ 'name' ] ) )
{
echo "A File With That Name Already Exists!<br>" ;
exit;
}
$res = copy ( $HTTP_POST_FILES [ 'userfile' ] [ 'tmp_name' ] , $path .
$HTTP_POST_FILES [ 'userfile' ] [ 'name' ] ) ;
if ( !$res ) {
echo "Upload Failed!<br>" ;
exit;
}
else {
echo "Upload Sucessful!<br>" ;
}
echo "File Name: " .$HTTP_POST_FILES [ 'userfile' ] [ 'name' ] . "<br>" ;
echo "File Size: " .$HTTP_POST_FILES [ 'userfile' ] [ 'size' ] . " bytes<br>" ;
echo "File Type: " .$HTTP_POST_FILES [ 'userfile' ] [ 'type' ] . "<br>" ;
echo "<a href=$path" .$HTTP_POST_FILES [ 'userfile' ] [ 'name' ] . ">View Image</a>" ;
}
else
{
echo "Wrong File Type<br>" ;
exit;
}
}
?>