• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have a problem with file uploading. I'm using that very method described in THIS article. I downloaded the whole script published by the author from HERE.
The application is meant to load files through HTML5 drag&drop and then, through javascript, send them to serverside by ajax request.
Everything works fine but the problem occurs when I want to read a parameter from AJAX request header. Here's the code of PHP "file reader":

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

if ($fn) {
//above there is the upload of the file with file_put_contents function
//which actually works fine when I replace $fn with my own value and ommit the "if" condition

Earlier, the XMLHttpRequest.setRequestHeader method is launched to set "X_FILENAME" header. Here's the javascript:

var xhr = new XMLHttpRequest();

// start upload
xhr.open("POST", $id("upload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);

And here's the proof (a crop from my Chrome's "firebug"):

When I var_dump the $fn variable in PHP it returns boolean FALSE. What is wrong?

P.S. I'm using XAMPP v1.8.1 with Apache 2.4.3 and PHP 5.4.7 on Win7 x64. I'm running the site on the latest Chrome. As you can guess the site is running on localhost. I didn't change anything in php.ini file - everything is set to default.

Underscores don't seem to be valid characters for header names. Use Hyphens and it will work beautifully.

xhr.setRequestHeader("X-FILENAME", file.name);

and no changes are required in your PHP.

  • 17
Reply Report
      • 2
    • Your solution works great! Thank you. At the first approach I didn't notice the last line in your comment and though I'm doomed.

New versions of Apache and PHP 5.3

   $UploadDir=dirname(__FILE__).'/'.;
   $Datos_cabecera=getallheaders();
    $fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
    if (!$fn) {$fn=$Datos_cabecera['X_FILENAME'];} //New versions of Apache and PHP 5.3
    if ($fn) {
    file_put_contents($UploadDir.$fn,file_get_contents('php://input'));
    }
  • -1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions