Home > other >  Uploaded file not saving in the directory (PHP AJAX) "Serious in Need"
Uploaded file not saving in the directory (PHP AJAX) "Serious in Need"

Time:10-19

So i try this simple upload code but it doesn't work properly for me. As cFos Speed shows the file uploading process is OK and 150kb pic is uploading, but at the end the file is not in the directory. Also mkdir doesn't create a new folder. although i gave all permissions such az rwx chmod and apache:apache chown completely to the files i'm working on. Here's the codes for html and ajax :

<input type="file" name="authform" id="authform" accept="image/png">

and

$(document).ready(function()
{
        $("#authform").on("change", function()
        {
                var input = $(this);
                var inputLength = input[0].files.length;
                var file;
                var formData = new FormData();
                for (var i = 0; i < inputLength; i  )
                {
                        file = input[0].files[i];
                        formData.append( 'authform[]', file);
                };
                        $.ajax({
                        url: "file_upload.php",
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
});

and for the file_upload.php:

<?php
if (!file_exists('uploads')) {
    mkdir('uploads', 0777);
}

$filename = time()."_".$_FILES['file']['name'];

 if ( 0 < $_FILES['file']['error'])  {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        move_uploaded_file($_FILES['file']['tmp_name'], 'downloads/user-files/' . $filename);
   }

echo 'downloads/user-files/'.$filename;
die;
?>

And Yes! i used a different directory because as said before mkdir does not work... So what do you suggest guys?

CodePudding user response:

Your file input does not have the multiple attribute so there is no need to loop through it as it will only have one file associated with it.

    $("#authform").on("change", function()
    {
            var formData = new FormData();
            formData.append('authform', this.files[0]);
            $.ajax({
                url: "file_upload.php",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false
            });
    });

In your php code you refer to the file with the parameter file but you set it to authform in the js.

<?php
if (!file_exists('uploads')) {
    mkdir('uploads', 0777);
}

$filename = time()."_".$_FILES['authform']['name'];

 if ( 0 < $_FILES['file']['error'])  {
        echo 'Error: ' . $_FILES['authform']['error'] . '<br>';
    }
    else {
        move_uploaded_file($_FILES['authform']['tmp_name'], 'downloads/user-files/' . $filename);
   }

echo 'downloads/user-files/'.$filename;
die;
?>

CodePudding user response:

The answer was i should have done this with copy() instead of move_uploaded_file(). I only needed a Form with enctype="multipart/form-data" and input with type="file", then with an submit action do this :

copy($_FILES['authform']['tmp_name'], "/var/www/html/test/app/downloads/user-files/" . $_FILES['authform']['name']);

And that's it !!! Works like a gem !

This is for anyone who has the same problem that's a good solution, although i still don't know how to fix the ajax problem but this just do the thing...

And to the person with more than 150rep who downvoted my question, i'm tryna being polite here so just don't be toxic and if you can help, just help people instead of smashing.

  • Related