On the change of an input type file, I'm attempting to upload the file via ajax (not via form submit), move the file to a temp directory of my choosing, pass back then the path to that temp file then process. I'm using CentOS Web Panel as the platform, my ajax code works correctly
$(document).on("change", '.dropify', function() {
var myFormData = new FormData();
var fil = this.files[0];
myFormData.append('file', fil);
console.log(myFormData);
$.ajax({
url: 'classes/images/upload.php',
type: 'POST',
dataType: 'text',
contentType: false,
processData: false,
data: myFormData,
success: function(response) {
console.log(response);
}
});
});
When this file gets to PHP it is going into the sys_temp_dir that I cannot move the file from via move_uploaded_file(), how can I get this to use the upload_tmp_dir? or force it to upload to another directory entirely? I'd rather not use the sys temp dir as I have sessions etc living in it. PHP below for reference.
if (isset($_FILES['file']['name'])) {
if (0 < $_FILES['file']['error']) {
echo 'Error during file upload' . $_FILES['file']['error'];
} else {
if (file_exists('images/uploads/' . $_FILES['file']['name'])) {
echo 'File already exists : images/uploads/' . $_FILES['file']['name'];
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'images/uploads/' . $_FILES['file']['name']);
echo 'File successfully uploaded : images/uploads/' . $_FILES['file']['name'];
}
}
} else {
echo 'Please choose a file';
}
example of error log below
[Mon Jul 17 13:01:25.742990 2017] [:error] [pid 212450:tid 139781212186368] [client 192.168.10.88:56517] PHP Warning: move_uploaded_file(images/uploads/logo.jpg): failed to open stream: No such file or directory in /home//public_html/classes/images/upload.php on line 13, referer: http:///fragrances-new.php?
[Mon Jul 17 13:01:25.743071 2017] [:error] [pid 212450:tid 139781212186368] [client 192.168.10.88:56517] PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpcvmedN' to 'images/uploads/logo.jpg' in /home//public_html/classes/images/upload.php on line 13, referer: http:///fragrances-new.php?
images/exists in the same folder as your script, use:__DIR__ . '/images/uploads/instead. (__DIR__will give you the absolute path to the file it's written in). If your file gets included in another file, then all relevant paths will be relevant from the file that does the including, not the included file.move_uploaded_file()returns and also dump the full path to the moved file and see that all looks correct.upload_tmp_dir?” – by specifying a value for that setting in your PHP configuration. And FYI, your code is dangerous, because you trust the file name as it is send by the client. You need to go read up on what is called path traversal attacks.