0

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?

8
  • 1
    Why can't you move it? Errors? Commented Jul 17, 2017 at 12:59
  • @JayBlanchard updated with error Commented Jul 17, 2017 at 13:03
  • 2
    Change your paths to use absolute paths. If the folder 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. Commented Jul 17, 2017 at 13:03
  • Check what move_uploaded_file() returns and also dump the full path to the moved file and see that all looks correct. Commented Jul 17, 2017 at 13:17
  • 1
    “how can I get this to use the 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. Commented Jul 17, 2017 at 13:18

2 Answers 2

1

You can try this to move file from one folder to another

if(!is_dir($destination_folder)) {
   mkdir($destination_folder, 0777,true);
}

if(copy($source_folder, $destination_folder)) {
   unlink($source_folder);
}

It works for me..

Sign up to request clarification or add additional context in comments.

1 Comment

this was not the nature of the question, problem was solved in comments
0

you just set your js file like this,then you will get all form data

 $(document).on("change", '.dropify', function() { 
   var myFormData = new FormData(document.getElementById("form-id"));


    $.ajax({ 
        url: 'classes/images/upload.php',
        type: 'POST',
        dataType: 'text',
        contentType: false,
        processData: false,
        data: myFormData,
        success: function(response) { 
            console.log(response);
        }

    });

});

1 Comment

The OP's issue isn't uploading the file, but rather moving the file in the back end, after it's been uploaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.