Upload files using ajax - file / directory not found

I have a form that allows me to submit text + number of files. a form submitted in AJAX.

Since this is the number of files, my upload function is giving me an error:

Warning: move_uploaded_file (images / usersFiles / 14367317720-101.JPG) [function.move-uploaded-file]: unable to open stream: no such file or in directory C: \ Program Files (x86) \ wamp \ www \ new- site \ func \ global.func.php on line 134

line 134:

if (move_uploaded_file($files['file']['tmp_name'][$i], USER_FILES.$files['file']['name'][$i]))

      

files "var should be an array (because I can load the number of files).

How can I fix the error?

HTML:

<form class="form-horizontal" action='#' method="post" id="addCommentForm" enctype="multipart/form-data">

    <textarea class="form-control" name="post[text]"></textarea>
    <input type='file' name='file[]' class='multi form-control' maxlength='1' accept='gif|jpg|png|bmp' id="files"/>
    <a class="btn btn-primary" id="submit">submit</a>

</form>

      

JS:

$(function() {
    $("#submit").click(function() {

        var file_data = $('#files').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file[]', file_data);
        var files_data =  form_data;

        var act = 'add';
        form_data.append('act', act);
        form_data.append('post[text]',  $("#addCommentForm").find("textarea").val());   

        $.ajax({
               type: "POST",
               url: "ajax/addPost.php",

               dataType: 'text',  
               cache: false,
               contentType: false,
               processData: false,  

               data: form_data,
               success: function(data)
               {
                $('#commentsBox').html(data);
                $("#addCommentForm")[0].reset(); 
               }

             });

        return false; // avoid to execute the actual submit of the form.
    });
});

      

Server:

function upload_files ($ownerID, $msg, $files, $type)
{

    $dateLX = get_current_linuxTime();

    ///////// Upload files //////////////
    if(!empty($files))
    {

        foreach($files['file']['name'] as $i => $fileName)
        {
            $fileSurffix = pathinfo ($_FILES['file']['name'][$i]);
            $fileSurffix = $fileSurffix['extension'];

            $files['file']['name'][$i] = str_replace(' ','',$files['file']['name'][$i]);
            $files['file']['name'][$i] = $dateLX.$i."-".$ownerID.".".$fileSurffix;
            $fileName = $files['file']['name'][$i];

            if (move_uploaded_file($files['file']['tmp_name'][$i], USER_FILES.$files['file']['name'][$i]))
            {
                $uploadFilesQuery = "INSERT INTO `files` (ownerID, name, type)
                                        VALUES('$ownerID', '$fileName', '$type')";

                $res = mysql_query($uploadFilesQuery);
                if (!$res)
                    $msg['error']['uploadFile'] = "error <br />".mysql_error();
            }
            elseif ($files['file']['error'][$i] != 4)  
                $msg['error']['uploadFile'] = "ERROR ";


        }   
    }
    return ($msg);  

}

      

+3


source to share


1 answer


King, do a var_dump in your php file, on the line before "move_uploaded_file" like this:

echo "<pre>";
var_dump($files);
echo "</pre>";

      

If your form does indeed submit more than one file, you need to change the position [$ i], because "file" is an array:

move_uploaded_file($files['file'][$i]['tmp_name'], USER_FILES.$files['file'][$i]['name'])

      

But if your form only publishes one file, you have to change the form (remove the [] in the input name):



<input type='file' name='file' class='multi form-control' maxlength='1' accept='gif|jpg|png|bmp' id="files"/>

      

And php (removing [$ i] on these lines:

move_uploaded_file($files['file']['tmp_name'], USER_FILES.$files['file']['name'])

      

Is it working now?

0


source







All Articles