PHP: Upload file

8

I'm here with a problem uploading an XML file in PHP.

I have a form that allows me to select a file:

<form id="upload" action='' method='post' enctype="multipart/form-data" >
     <input id='uploaded_file' type='file' name='uploaded_file' title="Procurar ficheiro" /> 
     <input class="btn btn-primary" type="submit" name ="submitA_" value="Upload"/>
</form>

I have the code to read the file (through the DOM) and import the data into the DB:

$uploaded  = (object) $_FILES['uploaded_file']; //line 42
$import->dom->load( $uploaded->tmp_name );
$import->updateDB();

What happens is when the file is relatively large (9MB), the object $uploaded->tmp_name is empty and, as a consequence, upload is not done for the DB, in> warning of PHP:

  Warning: DOMDocument :: load (): Empty string supplied as input on line 42

The $uploaded->error is NULL and the result of var_dump($uploaded); is:

  

object (stdClass) # 2 (0) {}

Note: In% with% I have php.ini and upload_max_filesize = 128M

    
asked by anonymous 12.05.2014 / 18:52

2 answers

3

The problem is in the PHP configuration ( php.ini ):

post_max_size = 8M

If the size of the posted data is greater than post_max_size then the super $_POST and $_FILES are empty.

Here's the tip to check when uploading files in PHP always check in php.ini :

Always taking into account that the size of post_max_size must be greater than upload_max_filesize . If memory limit is active in your script it should be greater than post_max_size .

Based on SOEN response and PHP Documentation

    
13.05.2014 / 11:54
2

Initially I had not been able to reproduce its error, with its code slightly adapted. Here I got:

  DOMDocument :: load (): I / O warning: failed to load external entity

Although it is a clear message from the point of view input / output does not help much when the context is uploaded.

According to this bug reported, # does not take into account the include_path which requires that the path absolute is informed, which makes no sense since the value of that $ _FILES entry is already absolute.

For stubbornness, I passed the amount to be informed to the DOM by realpath() . Even though realpath did not affect the data, that is, it continued with absolute correct, I was able to reproduce its error. Maybe even because DOM forces cast to string ( (string) FALSE ) which generates an empty string.

I went behind the temporary file and it did not exist (!). I re-sent the file, and it worked, but the file was still gone.

With all these problems, the only plausible and hitherto unplanned solution was to get back to the basics of uploading files and REALLY uploading the file.

Given the educational case in question, I did not worry too much about security and just did it:

if( $uploaded -> error == ERR_OK ) {

    move_uploaded_file( $uploaded -> tmp_name, './uploaded.xml' );
}

And I passed the uploaded file, here named uploaded.xml , to the DOM method:

$dom = new DOMDocument;

$dom -> load( './uploaded.xml' );
    
12.05.2014 / 21:33