Reputation: 675
I have a folder called uploads
, and files called, upload.html
and upload.php
. My upload.html
looks like this (well, at least the relevant part does):
$(document).ready(function(){
$("#pictureUploadSubmit").submit(function(event) {
var file_data = $('#fileToUpload').prop('files')[0];
var form_data = new FormData();
form_data.append('fileToUpload', file_data);
alert(form_data);
$.ajax({
url: 'upload.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
event.preventDefault();
});
});
And my upload.php
looks like this:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Now, the file seems to be uploading but I think it is being uploaded into an incorrect directory because when I try to upload the same file twice, it echoes, Sorry, file already exists
and when the image is successfully uploaded, it echoes "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."
. However, the files that are already in my FireFTP are not showing up in my uploads
folder.
I believe I have allowed for proper permission, namely, I allowed the read, write, and execute for my upload folder.
So I think it is a misdirection of my target directory
. I tried searching for the files that were uploaded and I think I checked all the directories, but I cannot find them. Anybody have a solution to this problem?
Upvotes: 1
Views: 432
Reputation: 56677
You are using a relative directory for uploads,
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
# ...
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
That will move the file to the uploads directory relative to wherever this script is running. Look in the directory where upload.php is, there may be an uploads directory there with your files.
If that isn't the case, I suggest using an absolute path for $target_dir
so you can be confident you know where it is going to place the final files.
Upvotes: 1