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 a 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 type 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 is a few point on 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 ency type must have to multipart/form-data

 

* the forms method should be post type

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

 

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

If the folder permission is not read write and execute 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

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.