LLLLLLL Posted December 19, 2013 Share Posted December 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284852-unable-to-get-error-message-from-move_uploaded_file/ Share on other sites More sharing options...
LLLLLLL Posted December 19, 2013 Author Share Posted December 19, 2013 (edited) 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 December 19, 2013 by timneu22 Quote Link to comment https://forums.phpfreaks.com/topic/284852-unable-to-get-error-message-from-move_uploaded_file/#findComment-1462756 Share on other sites More sharing options...
jazzman1 Posted December 19, 2013 Share Posted December 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284852-unable-to-get-error-message-from-move_uploaded_file/#findComment-1462761 Share on other sites More sharing options...
LLLLLLL Posted December 19, 2013 Author Share Posted December 19, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/284852-unable-to-get-error-message-from-move_uploaded_file/#findComment-1462762 Share on other sites More sharing options...
mac_gyver Posted December 19, 2013 Share Posted December 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284852-unable-to-get-error-message-from-move_uploaded_file/#findComment-1462766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.