1

I have created FormData() that work very well, because i test it using jQuery.ajax. but cause i want to have progress bar for uploading, i could not create it when i use jQuery.ajax . so i decided to use ajax directly and post form data. but getting form data using php is not work.

javascript

xhr.open("POST","assets/php/upload.php?action=uploadFiles");
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.send(newFormData);

php

print_r($_FILES); //output, empty array

This is my question, why cannot i get form data using php?

EDIT::

this is my try to make progress bar using jquery but it did not work very well. the progress bar will be complete very fast and the file is not uploaded yet.

function updateProgress(evt)
{
    // evt is an ProgressEvent.
    if (evt.lengthComputable)
    {
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
        // Increase the progress bar length.
        $(".progress > div").css(
        {
            width: percentLoaded + '%'
        });

    }
}

$.ajax(
{
    url: 'assets/php/upload.php?action=uploadFiles',
    type: 'POST',
    data: newFormData,
    cache: false,
    xhr: function ()
    {
        myXhr = $.ajaxSettings.xhr();
        if (myXhr.upload)
        {
            myXhr.upload.addEventListener('progress', updateProgress, false);
        }
        return myXhr;
    },
    contentType: false,
    processData: false,
});

EDIT::

I finally found why i cannot get ajax request using $_FILES. that occurred because i set content type, now if i remove it from request, i can get ajax request. but now the progress bar will be complete soon while uploading file has not finished yet. where is the problem with my code?

thank you, Alireza

  • You are trying to upload a file using AJAX, http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – Ibu Sep 18 '12 at 19:18
  • thank you, but i tried that before. that did not work well. the progress bar will complete very fast and the file did not upload yet. i have edited my question, please loo that again. thank you –  Sep 19 '12 at 07:32

3 Answers3

0

Actually I don't see anything that produces $_FILES there. and something even more important YOU CANT USE AJAX DIRECTLY IN PHP... If you use javascript then what is the print_r($_FILES) thing? the Browser sends the AJAX request not the php. so when the borwser has loaded the page you can't directly read the output with $_FILES.. your other page should read the $_FILES... And to upload using AJAX use jsupload...

Miro Markaravanes
  • 3,285
  • 25
  • 32
  • sorry i did not understand what you mean. may you give me an example or something like that? thank you –  Sep 19 '12 at 06:30
0

You could use plupload to do this.

An example that covers how to update an element with a procent is here http://www.plupload.com/example_custom.php

If you download the module it also contains the php code that works with chunks (that's what you need to accomplish the file upload behavior you want ).

Perfection
  • 199
  • 1
  • 10
  • Thank you for for your response, but i do not want to use plugin or something like that, i just want to know the answer of my question and write my script. –  Sep 19 '12 at 08:34
0

Check HTML5-powered Ajax file uploads and this project: HTML5 AJAX File Uploader.

This file provides a JavaScript object to simplify utilization of the HTML5 File API for AJAX uploading.

Renan
  • 462
  • 7
  • 24
zoghal
  • 31
  • 1
  • You know what Saleh, now the problem is the progress will be completed while file uploading has not finished yet. –  Sep 22 '12 at 16:13
  • if you are used testing on localhost, for test used a big file,so you see the progress is correct – zoghal Sep 22 '12 at 22:02
  • oh yeah i am using my script on localhost but files will be uploaded on a server via ftp. so the progress cannot be complete while uploading has not finished yet. - EDIT: you are right. i've just tested it and the progress works very well. thank you Saleh. –  Sep 22 '12 at 23:21