1

I have a problem with saving photos with Ajax and PHP.

This is my form in HTML

<form id="form3">
    <input id="photo3" name="photo" class="form-control" type="file" id="fileInput3" />
</form>

And this is my JS

data = $("form#form3").serializeArray();
    var file = $("#photo3")[0].files[0];
    data.push({name: "photo", value: file});
    $.ajax({
        url: 'registrace.php',
        data: data,
        complete: function (response) {
            alert(response.responseText);
        },
        error: function () {}
    });

And this is PHP

$output = 'users/'.$namernd.'.jpg';
move_uploaded_file($_GET['photo'],$output);

Everything is copied to my database and works, but photo is not saved to my server.

Adam Boduch
  • 11,023
  • 3
  • 30
  • 38
Martin K.
  • 131
  • 1
  • 14
  • Is the users directory writable ? – Akshay Sep 26 '15 at 18:04
  • I have it on XAMPP on my Mac, but i set to folder Write&Read to everyone, so i think it is writable :) – Martin K. Sep 26 '15 at 18:10
  • That js looks incomplete. Try to post as FormData. Some samples https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects FormData with jQuery http://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery – featherbits Sep 26 '15 at 18:15
  • 2
    FIle data **never** will be in `GET` – u_mulder Sep 26 '15 at 18:45

1 Answers1

2

Ok, i found solution. Just change to this:

var formdata = new FormData();
    formdata.append('name', $('#name3'));
    formdata.append('password', $('#password3'));
    formdata.append('city', $('#city3'));
    formdata.append('email', $('#email3'));
    formdata.append('file', $('#photo3')[0].files[0]);

And

$namernd = uniqid();
$output = 'users/'.$namernd.'.jpg';
move_uploaded_file($_FILES["file"]["tmp_name"],$output);
Martin K.
  • 131
  • 1
  • 14