What if I have no way of getting the input file:
<input type="file" name="upload" id="upload">
After choosing the file I want to upload, the input field will disappear. Instead, it will display the absolute path:
C:\users\foo\Desktop\file.zip
C:\fakepath\file.zip
Here's the code I used to get the absolute path:
<script>
$('#upload').on('change',function(){
var filename = document.getElementById("filename").innerHTML;
$.ajax({
type: "POST",
url: "execs/upload.php",
data: { filename: filename},
dataType: "json",
success: function (data) {
alert ("Success")
},
error: function () {
alert ("Failed")
}
});
})
</script>
Will I still be able to upload it in PHP? Most of what I get online is that I will need $_FILES['filename']['tmp_name']. I don't know how I'll get it if I only have the absolute path.
This is the upload.php file:
<?php
$filename = $_POST["filename"]; //C:\users\foo\Desktop\file.zip
$target_dir = "uploads/";
$target_file = $target_dir . $filename;
if(move_uploaded_file($filename, $target_file)){ // $target_file = uploads/file.zip
echo "yes";
}
else echo "no";
?>
When I also checked if the file exists ($filename), it says it does NOT.
Any help would be very much appreciated! Thanks a lot!