Friday , March 22 2019
Home / PHP / How to upload file on server using php ?

How to upload file on server using php ?

We can easily upload a file or image on the server using this php code.
Before upload an image or file we should configure php.ini file.
In your “php.ini” file, search the file_uploads directive, and change it to On:

file_uploads = On

Firstly create an html file:  upload_file.html

<!DOCTYPE html>

<form action=”upload_file.php” method=”post” enctype=”multipart/form-data”>
<input type=”file” name=”fileUpload” id=”fileUpload”>
<input type=”submit” value=”Upload Image” name=”submit”>



Now create a ohp file neame: upload_file.php

$target_dir = “uploads/”;
$target_file = $target_dir . basename($_FILES[“fileUpload”][“name”]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST[“submit”])) {
$check = getimagesize($_FILES[“fileUpload”][“tmp_name”]);
if($check !== false) {
echo “File is an image – ” . $check[“mime”] . “.”;
$uploadOk = 1;
} else {
echo “File is not an image.”;
$uploadOk = 0;

About admin

Check Also

Resize Images in php

Creating thumbnails of the images is required many a times, this code will be useful ...

Leave a Reply