0

I'm trying to code my own php uploader so I can learn and understand how it all works. Trying not to use any libraries/plugins/API's. So basically I have the javascript stuff doin what it's supposed to be (i hope..). Now I'm stuck on the PHP. I want it to upload and save it as img/1.png. (not using any variables just to test it...). Here are my scripts. Keeping it very simple without any error checks just to get basic uploading working. Would this work for multiple files? May somebody guide me on what my PHP file should look like just for very basic (multi)file upload? Thanks!

album_create.js

$(document).ready(function() {
  $('#album_create').submit(function() {
    var file = document.getElementById('album-files').files[0]; //Files[0] = 1st file
    var reader = new FileReader();

    reader.onload = function(f) {shipOff(f);};
    reader.onerror = function(event) {
      console.error("File could not be read! Code " + event.target.error.code);
    };
    reader.readAsText(file, 'UTF-8');
    return false;
  });
});


function shipOff(event) {
  var result = event.target.result;
  var fileName = document.getElementById('album-files').files[0].name; //Should be 'picture.jpg'
  console.log(fileName);
  $.post('ajax/album_create.php', { data: result, name: fileName }, function(data){console.log(data);});
  return false;
}

ajax/album_create.php

$data = $_POST['data'];

move_uploaded_file($data[0], 'img/1.png');

1 Answer 1

1

you are posting:

data: result, name: fileName 

but album_create.php expecting:

    $data = $_POST['data'];
    $fileName = $_POST['fileName'];

$fileName = $_POST['fileName']; should be: $fileName = $_POST['name'];

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

2 Comments

good catch. i changed it to $_POST['name'] but it still doesn't work. I don't actually use $fileName anywhere. am I passing in incorrect values as arguments in the move_uploaded_file function? I tried $data and $data[0] as the first argument but they both don't work.
use Firebug console to see exactly what do you post

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.