Jump to content

Unable to get error message from move_uploaded_file


LLLLLLL

Recommended Posts

I'm trying to test error conditions for file uploads, but when a file intentionally fails, I'm not even sure how to access the code. Here's a snippet:

	$file_arr = $_FILES[ 'digifile' ];
	$fn       = $file_arr[ 'name' ];
	$temp     = $file_arr[ 'tmp_name' ];
	$error    = $file_arr[ 'error' ];
	error_log( 'the error is: ' . $error ); // ** SEE NOTE BELOW
	
	if ( $error > 0 ) {
		$ui->upload_message = "Error during file upload. ";
		
		// try to switch the error to show a relevant message


	} else {			
		if ( move_uploaded_file( $temp, $full_path ) )
			// something
	}

My test for now is simple... test a small image file, it's fine. But I have set my max file size to 16MB in php.ini, and I'm intentionally uploading a 19MB file. What is the expected result?

 

Well I can tell you that the result is this code IS NOT REACHED AT ALL! How is that possible? error_log() doesn't get reached. I don't really know what happens when there is an error, but I'd like to show a customer a relevant message if this happened in the real world. There's no boolean result of move_uploaded_file, either, since like I said THE CODE ISN'T REACHED.

 

I really don't understand. Any help is appreciated.

 

Link to comment
Share on other sites

I tried to edit the original post to no avail. Here's clearer information:

 

My test for now is simple... test upload a small image file, it's fine. But I have set my max file size to 16MB in php.ini, and I'm intentionally uploading a 19MB file. What is the expected result?

 

Here's what I've found:

1) If the php.ini post size ( 8 ) is smaller than the max file size (16), $_POST is empty and so is $_FILES. There's no way to get an error

2) If the php.ini post size (20) is larger than the max file size (20), $_FILES[ 'whatever' ][ 'file' ] gives me a correct result.

 

Is there a way to get an error that helps a user in scenario #1?

Edited by timneu22
Link to comment
Share on other sites

 

My test for now is simple... test upload a small image file, it's fine. But I have set my max file size to 16MB in php.ini, and I'm intentionally uploading a 19MB file. What is the expected result?

 

It's imposibble! Did you restart your web server to make those changes available?

 

 

 

1) If the php.ini post size ( 8 ) is smaller than the max file size (16), $_POST is empty and so is $_FILES. There's no way to get an error

 

Check this out -> http://www.php.net/manual/en/features.file-upload.errors.php

 

 

2) If the php.ini post size (20) is larger than the max file size (20), $_FILES[ 'whatever' ][ 'file' ] gives me a correct result.

 

Yes, that's correct. The post max size should be greater or equal to max file size.

 

 

 

Is there a way to get an error that helps a user in scenario #1?

 

Use javascript to inform the client if the file is to large, so don't waste the server's resources. 

Link to comment
Share on other sites

You cited the load errors list for:

 

1) If the php.ini post size ( 8 ) is smaller than the max file size (16), $_POST is empty and so is $_FILES. There's no way to get an error

 

 

But that doesn't really help answer the question. There IS NO ERROR because $_POST and $_FILES are empty. I realize this is potentially a server issue, but a customer had this issue. Is there any way to know that a post was attempted?

Link to comment
Share on other sites

the recommendation is to use $_SERVER['REQUEST_METHOD'] == "POST" to detect if an upload form has been submitted. then, if the $_FILES array is empty, the file size likely has exceeded the post max size setting. you can further check the $_SERVER['CONTENT_LENGTH'] value and do your own comparisons with the maximum size settings to output your own user message.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.