PHP File Upload – how to upload a file in php

PHP File Upload - how to upload a file in php

PHP File Upload – how to upload a file in PHP

The PHP File Upload process is worked while a file is sent from an HTML form and after submission of the form the file is saved in a temp location ( $_FILES[‘image’][‘tmp_name’] ) then using the PHP function move_uploaded_file() you van be able to upload the file from temp location to a folder of your host where your project files are located, we are going to learn how to upload a file in PHP.

you can also make a limit on file types like image or dock or pdf, and also you can bound the upload size of the file.

Things to remember on PHP File Upload function

there are a few points on the PHP File Upload function to remember as if there is no error in your code but the file may not be uploaded due to missing of the points. and they are…

 

* the forms enctype must be multipart/form-data

as the file type input use to send an array so the forms encytype must have to multipart/form-data

 

* the forms method should be a post type

as the size of the image may be huge and it may not be sent through the URL, so the method of the form should be a post type.

 

* Upload folder permission should be read-write and execute or 777

If the folder permission is not read-write and executes or 777 where you are uploading the image the program can not be able to upload files.

Example of php file upload:



<?php

if(isset($_FILES[‘image’])){
$err= array();
$file_name = $_FILES[‘image’][‘name’];
$file_size = $_FILES[‘image’][‘size’];
$file_tmp_name = $_FILES[‘image’][‘tmp_name’];
$file_type= $_FILES[‘image’][‘type’];
$file_extense = strtolower(end(explode(‘.’,$_FILES[‘image’][‘name’])));

$extensions= array(“jpeg”,”jpg”,”png”,”gif”,”JPEG”,”JPG”,”PNG”,”GIF”);

if(in_array($file_extense,$extensions)=== false){
$err[]=” jpeg , jpg , png , gif files are only alowed.”;
}

if($file_size >= 2097152){
$err[]=’File size must be equal or below 2 MB’;
}

if(sizeof($err)==0){
move_uploaded_file($file_tmp_name,”images/”.$file_name);
echo “File successfully uploaded”;
}else{
foreach ($err as $key => $value) {
echo $value . “<br/>”;
}
}
}
?>
<html>
<body>

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

</body>
</html>

In the above example of php file upload function the keys are as described below.

* $_FILES[‘image’][‘name’] provides the name of the file with extension like logo.png
* $_FILES[‘image’][‘size’] provides the size of the submitted file in Byte
* $_FILES[‘image’][‘tmp_name’] is the file temporary saved
* $_FILES[‘image’][‘type’]provides the type of the file
* strtolower(end(explode(‘.’,$_FILES[‘image’][‘name’]))) breking the file name by . (dot) and providing the end point or extension of the file
* array(“jpeg”,”jpg”,”png”,”gif”,”JPEG”,”JPG”,”PNG”,”GIF”) making an array of extension to allow for upload

and in move_uploaded_file($file_tmp_name,”images/”.$file_name) function there is passing 2 parameters the first one is the temporary located file and the second one is where to save the uploaded file.

Hope you got a clear idea on PHP File Upload and you have done it by yourself and learned how to upload a file in PHP,

Thanks for reading

Share The Post On -