PHP : Upload File

Asked

Viewed 312 times

9

I’m here with a problem making one upload of 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 to the BD:

$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 left empty and, consequently, the upload for the comic book, below is the Warning of PHP:

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

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

Object(stdClass)#2 (0) { }

Note: No php.ini have upload_max_filesize = 128M and max_execution_time = 60

  • Maybe your problem is the configuration of PHP, check the upload_max_filesize, what value is configured?

  • That’s not the problem because I have the same server uploading up to 128 MB file

  • What have you got in $uploaded->error? If possible show the result of var_dump($uploaded);.

  • 1

    Check the script timeout if it is not "popping". It may be that the problem is in the upload time, not in the size. http://www.php.net/manual/en/function.set-time-limit.php

  • By the way, it would be nice to have a more descriptive title for the problem, what do you think? (besides the remaining Html5, dom and xml tags). Jaja I delete this comment here. It was part of the top, but it’s two separate subjects, and this one is just "guess".

  • @Bacco I have the max_execution_time = 60 that arrives perfectly since not even 20 seconds it takes to read the file. A title type which?

  • @Vieira already has the result of var_dump and of $uploaded->error in the question.

  • I’ve managed to find the problem: reply&#Thanks for the help.

Show 3 more comments

2 answers

4


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

post_max_size = 8M

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

Here is the tip to check when you have problems uploading PHP files always check in php.ini:

Always bearing in mind that, the size of the post_max_size should be greater than upload_max_filesize. If memory limit is active in your script should be greater than post_max_size.

Based on response from the SOEN and in the PHP documentation

2

Initially I had not managed to reproduce its error, with its slightly adapted code. 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 doesn’t help much when the context is uploaded.

According to that bug reported, Domdocument::load() does not take into account the include_path which requires the path absolute be informed, which makes no sense since the value of this $_FILES input is already absolute.

For stubbornness, I passed the value to be informed to DOM by realpath(). Even though realpathhas not affected the data, that is, continued with path absolute correct, I was able to reproduce its error. Perhaps even because the DOM forces the cast for string ((string) FALSE) which generates an empty string.

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

With all these problems, the only plausible and hitherto unforeseen-free solution was to go back to uploading files and REALLY upload the file.

Given the didactic case at hand, I didn’t care much for safety and I just did that:

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

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

And I passed the file sent, here named uploaded.xml, for the FOD method:

$dom = new DOMDocument;

$dom -> load( './uploaded.xml' );
  • The problem is that I can’t really save to the server, that the file will then be sent to a cloud

  • It is even safer for you to store on your server and then move to the cloud, suddenly even in the same Request. Or, if the cloud you hired has some kind of API, you can upload it directly to it.

  • Yes, but I need to store it first in the comic book. And I can’t store it on the server, it has to be read on time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.