PHP uploading files image only
I am not able upload my image file to server, using XAMMP phpmyadmin and apache services. I'm wondering where I did wrong.
I have to use this kind of security code to prevent any extension upload that can exploit by hacker.
It just shows that the files is "Your image was not uploaded" as an error code.
Not sure if it is because I didn't specify the root of the website? If yes, how can I specify?
<html>
<head>
<?php
if(isset($_POST['submit']))
{
$target_dir = '/uploads/';
$target_dir1 = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadedname=$_FILES['fileToUpload']['name'];
$uploadedname_temp=$_FILES['fileToUpload']['name'];
$uploaded_ext=substr($uploadedname_temp,strrpos($uploadedname_temp,'.')+1);
$uploaded_size=$_FILES['fileToUpload']['size'];
if(isset($_POST['submit']))
{
if (($uploaded_ext == "jpg" || $uploaded_ext == "JPG" || $uploaded_ext == "jpeg" || $uploaded_ext == "JPEG") && ($uploaded_size < 1000000)){
if(!move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir1)) {
echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';
} else {
echo '<pre>';
echo $target_dir1. ' succesfully uploaded!';
echo '</pre>';
}
}
else{
echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';
}
}
}
?>
</head>
<body>
<form action="uploadfiles.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Answer
Solution:
I tried below code. it is working properly. As there was issue with
$uploaded_size
you have not declared size variable and used that.Answer
Solution:
You are trying to move the file by it's original file name. However, when it gets uploaded, there is a
['tmp_name']
. When youmove_uploaded_file()
, you need to refer to the tmp name.to