Note that there are some explanatory texts on larger screens.

plurals
  1. POMissing Temp File in PHP from File Upload via. AJAX
    primarykey
    data
    text
    <p>I've been looking into uploading files via. AJAX using FormData and FileReader, but I keep running into the same problem.</p> <p>Upon triggering the file upload, it is passed to a PHP script which validates the uploaded file and attempts to move it into a permanent directory. Unfortunately, whilst validation is successful, calling <code>move_uploaded_file()</code> results in the following warnings:</p> <blockquote> <p><strong>Warning</strong>: move_uploaded_file(<em>[path\to\temp\directory]</em>\php256.tmp): failed to open stream: No such file or directory in <strong>[path/to/script/file.php]</strong> on line <strong>[line]</strong></p> <p><b>Warning</b>: move_uploaded_file(): Unable to move '<em>[path\to\temp\directory]</em>\php256.tmp' to '<em>[path\to\permanent\directory]\[filename.ext]</em>' in <strong>[path/to/script/file.php]</strong> on line <strong>[line]</strong><br /></p> </blockquote> <p>Uploading files via submitting a form normally and moving the temporary file works as expected.</p> <p>My php.ini settings are pretty standard:</p> <pre class="lang-none prettyprint-override"><code>file_uploads = On upload_tmp_dir = "[path\to\temp\directory]" upload_max_filesize = 2M max_file_uploads = 20 </code></pre> <p>My PHP should also be pretty standard:</p> <pre class="lang-php prettyprint-override"><code>$fileField = 'target_file'; if(!(isset($_FILES) &amp;&amp; is_array($_FILES) &amp;&amp; isset($_FILES[$fileField]))) { // No File Sent (Error Out) } $file = $_FILES[$fileField]; // Errors in transfer? if(isset($file['error'])) { $haltMessage = false; switch($file['error']) { /* Switchboard checking through all standard file upload errors. And, of course, ERR_OK */ } if($haltMessage !== false) { // An error occured! Error Out } } // Check if the file was uploaded. if(!is_uploaded_file($file['tmp_name'])) { // No File Uploaded (Error Out) } if(/* File type checking.. */)) { // Invalid file type (Error Out) } $uploadDir = 'path/to/permanent/dir'; $fileName = $file['name']; $location = $uploadDir . DS . basename($fileName); // DS = '/' $result = move_uploaded_file($file['tmp_name'], $location); if($result) { // Yes! } else { // Something did indeed go wrong. // This block of code is ran after the warnings are issued. } </code></pre> <p>So that leaves my JavaScript to be somewhat desired:</p> <pre class="lang-js prettyprint-override"><code>(function($) { $(document).ready(function(){ var url = 'url/to/php/script.php', input, formdata; // FormData support? (We have a fallback if not) if(window.FormData != null &amp;&amp; window.FileReader != null) { input = document.getElementById('target_file'); formdata = new FormData(), input.addEventListener('change', function(ev) { var reader, file; if(this.files.length === 1) { file = this.files[0]; if(!!file.type.match(/* File type matching */)) { reader = new FileReader(); reader.onloadend = function (e) { // Uploaded Handle } reader.onload = function() { formdata.append('target_file', file); $.ajax({ url : url, type : "POST", data : formdata, processData: false, contentType: false, success : function(res) { console.log(res); } }); } reader.readAsDataURL(file); } else { // Invalid file type. } } else { // No file / too many files (hack) selected. } }, false); } }); }(jQuery)); </code></pre> <p>I'm using FireFox 23.0.1, and I'm running PHP 5.4.7 on the server.</p> <p>Apologies for the long question, but I'm not sure where the problem seems to be. Dumping the $_FILES variable reveals an array that's full of the expected file information, including a tmp_name too. There's no sign of any error at that point, only when I attempt to move the temp file.</p> <p>Any help you can give in regards to this question would be much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload