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>
<html>
<body>
<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”>
</form>
</body>
</html>
Now create a ohp file neame: upload_file.php
<?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;
}
}
?>