I'm trying to upload a file via PHP using a form. Here is the PHP I have used in order to start the upload:
if(isset($_FILES['image'])){
$target_dir = "/var/www/html/uploads/";
$target_file = $target_dir . basename($_FILES["image"]["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["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
}
//if there is an error...
else
{
//set that to be the returned message
echo "Ooops! Your upload triggered the following error";
mysqli_stmt_close($stmt);
}
There are no errors/notices with
error_reporting(E_ALL);
ini_set('display_errors','On');
or in the apache2 error logs. The permissions of the folder in /var/www/html/uploads is 777 (for development only), so it is writeable.
you should move uploaded file to $target_dir. use move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir."new_file_name")
This is the way to upload file in the directory A folder named "UPLOAD" should be there in ht docs to see the uploaded files.
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"upload/".$file_name);
echo "Success";
}
else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>