0

To start things off, I've looked at a few similar problems on here but still can't resolve my issue here.

HTML:

<input type="file" name="filename" multiple="multiple" data-classIcon="icon-plus" data-buttonText="Upload Your File">

PHP:

$name = $_FILES['filename']; 
$temp_name = $_FILES['filename']['tmp_name']; 
if(isset($name)){
    if(!empty($name)){ 
        $location = 'uploads/'; 
        if(move_uploaded_file($temp_name, $location.$name)){
            echo 'uploaded';
        }
    } 
} else {
    echo 'error: not uploaded';
}

JS:

$('#cc-submit').on('click', function(e){

            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "balanced/example.php",
                data: $('#creditcardpost').serialize(),
                success: function(data)
                {
                    alert(data);                
                }

            });
        });

Error:

Undefined index: filename in /public_html/script.php on line xx (the two lines that collect the $_FILES variables.

"error: not uploaded"

IAteYourKitten
  • 968
  • 1
  • 6
  • 13
  • 2
    It seems $_FILES doesn't contain a key for 'filename'. Did you add `enctype=multipart/form-data` to your form tag? – Chris May 21 '14 at 21:38
  • I just added it, and the issue persists. :( – IAteYourKitten May 21 '14 at 21:43
  • Can you post your form tag? Can you also `print_r($_FILES)` and see what comes out? There is another thing you should change to your code btw. `isset()` checks the existence of a variable, $name always exists in your case, whether it's empty or not. Use `isset($_FILES['filename'])` instead. – Chris May 21 '14 at 21:44
  • 2
    Duplicate, see: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – jeroen May 21 '14 at 21:48
  • I just changed to isset($_FILES), then at the end used print_r($_FILES) and it returns an empty array: Array( – IAteYourKitten May 21 '14 at 21:49
  • @IAteYourKitten, `isset($_FILES)` will also always be true since it's a global variable. However, see jeroen's comment above, that's your problem. – Chris May 21 '14 at 21:50
  • I'm looking at that. It appears to be a very similar issue. I will check that and see if I can fix the issue. Thanks you guys. I'm always surprised with how quickly you can get help here. :) – IAteYourKitten May 21 '14 at 21:52

2 Answers2

0

You must use:

$name = $_FILES['filename']['name'];
Lucas Henrique
  • 1,380
  • 1
  • 11
  • 15
0

I used print_r on $_FILES and found my files were coming through as "file-0". I'm not doing multi file uploads, so this works for me. And here is the solution. PHP:

$name = $_FILES['file-0']['name'];  
$temp_name = $_FILES['file-0']['tmp_name'];  
        $location = '../uploads/';      
        if(move_uploaded_file($temp_name, $location.$name)){

        }

print_r($name);

And the JS that sends it needed to be worked a bit:

var data = new FormData();
jQuery.each($('#file')[0].files, function(i, file) {
    data.append('file-'+i, file);
});


$.ajax({
    url: 'balanced/upload.php',
    data: data,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
        $('#creditcardpost').append('<input type="hidden" value="' + data + '" name="filename" />');
    }
}); 

I've got it working. Thanks for helping out guys.

IAteYourKitten
  • 968
  • 1
  • 6
  • 13