Cyrus Bitangcol
Share This
//PHP File Upload Tutorial

PHP File Upload Tutorial

PHP File Upload Tutorial

In this tutorial we’ll create a simple php file image upload.
In this simple image file upload we will add a verification for file extension and size, making it a secure way to upload file.
You can use this for uploading images, pdf, zip file, rar, and doc, just make sure that you change the necessary part in the script.

HTML

<form action=”” enctype=”multipart/form-data” method=”POST”>
<input name=”image” type=”file” />
<input type=”submit” />
</form>

Make sure to add “enctype=”multipart/form-data” to form and type=”file” for the input

PHP

We will use “isset($_FILES[”])” to make sure some file is selected. $_FILES[‘ ‘] is an array with the file information. we will set the file name, size and tmp tmp_name is the temporary location where the file is uploaded

if(isset($_FILES[‘image’])){
$file_name = $_FILES[‘image’][‘name’];
$file_size =$_FILES[‘image’][‘size’];
$file_tmp =$_FILES[‘image’][‘tmp_name’];
$file_type=$_FILES[‘image’][‘type’];
$name=$_POST[‘name’];
$codes=$_POST[‘code’];
}

for the verification

$expensions= array(“jpeg”,”jpg”,”png”);

On this tutorial we need to allow the image extensions. You can add the appropriate extensions that you need. This is not recommended upload script you can change and modify it for your security, this is just for a simple tutorial purpose only.

if($file_size > 2097152){
$errors[]=’File size must be excately 2 MB’;
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,”images/”.$file_name);
echo “Success”;
}else{
print_r($errors);
}

For the full code download the file on http://codeninja.cyrusbitangcol.com

File password: “singlefileupload”

  • 0 Comment
Get in Touch
Close