spkvn
spkvn

Reputation: 194

File upload 302?

I've finally gotten my asynchronous file upload working, except it only seems to handle text files.

If I upload a text file, it works and saves the file. If I upload an image, it fails firebug reports the response has a 302 status, and I get sent to the root directory of my public_html

I thought it may be related to file size, but doesn't seem to be the case, as I made the text file larger than the image, and it worked out.

I'm not sure where to start troubleshooting, I'll place the PHP relevant snippet below.

PHP

$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME']:false);
echo $fn;

try{
    file_put_contents("upload/".$fn, file_get_contents("php://input"));
    //echo file_get_contents("upload/".$fn);
}catch(exception $e){
    echo $e->getMessage();
}

JS

function UploadFile(file){
    var xhr = new XMLHttpRequest();
    //on the below if
    //xhr.upload: returns a XMLHttpRequestUpload Object, I believe just to check if it's supported in broswer
    if (xhr.upload){
        xhr.open("POST","http://domain.com/absolutepath/what.php",true);
        xhr.setRequestHeader("X-FILENAME",file.name);

        xhr.onreadystatechange = function(){
        if(xhr.status == 200 && xhr.readyState == 4){
            callback(xhr.responseText);
        }
    }
    xhr.send(file);
    }else{
        alert("Sorry, don't think this is supported in your browser.");
    }
}

Is there a request header I should be setting? Is it something in php.ini I need to change? My max post size is I think 32MB, not sure what else would effect this.

Upvotes: 1

Views: 3562

Answers (1)

Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

302 is a status code, not a error code. It means that server is redirecting you to another page. But with status OK and Found.

The problem is usually caused by server having mod-security turned on and the filter engine filtering uploads.

To get around this add this code to your .htaccess file for your hosting account, if you are a client, or edit in apache configuration if you are administrator:

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

Upvotes: 3

Related Questions