Page 1 of 1

Excluding $_FILES error number 4

Posted: Wed Dec 07, 2011 1:12 pm
by rick.emmet
Hi Everyone,
I have a rather stupid question, but I haven't been able to find the correct method / syntax to do this. I want to allow users to upload a maximum of 5 photos - less then 5 is OK too. I've been testing the following script and haven't gotten it to ignore error # 4 (no file uploaded):

Code: Select all

  if ($_FILES['userfile']['error'] == 1 || 2 || 3 || 6 || 7)   {
	  
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])   {
		
      case 1:	echo 'File exceeded upload_max_filesize';
	  			break;
      case 2:	echo 'File exceeded max_file_size';
	  			break;
      case 3:	echo 'File only partially uploaded';
	  			break;
      case 6:   echo 'Cannot upload file: No temp directory specified.';
	  			break;
      case 7:   echo 'Upload failed: Cannot write to disk.';
	  			break;
    }
    exit;
  }
I will be using a while loop and incrementing through the script (this is simpler version). Does anyone know the proper syntax for the IF statement:
"if ($_FILES['userfile']['error'] == 1 || 2 || 3 || 6 || 7)" ?
Thanks much,
Rick

Re: Excluding $_FILES error number 4

Posted: Wed Dec 07, 2011 3:54 pm
by rick.emmet
I NEED MORE COFFEE!!!

I got this working - the code is here:

Code: Select all

if (in_array($_FILES['userfile']['error'], array (1,2,3,5,6,7)))   {
          
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])   {
                
      case 1:   echo 'File exceeded upload_max_filesize';
                                break;
      case 2:   echo 'File exceeded max_file_size';
                                break;
      case 3:   echo 'File only partially uploaded';
                                break;
      case 6:   echo 'Cannot upload file: No temp directory specified.';
                                break;
      case 7:   echo 'Upload failed: Cannot write to disk.';
                                break;
    }
    exit;
  }
Hope this is of some help to others!
Cheers,
Rick

Re: Excluding $_FILES error number 4

Posted: Fri Dec 09, 2011 11:23 am
by pickle
I want to mention a couple things:

You don't need that outer IF statement. You should always run through each possible error case. Write a case for "4" that doesn't do anything - but at least it will be handled. You should also have a default case for error values you don't know about.

You should also not be referencing 1,2,3,etc, but use the PHP constants instead. This will future proof you code in case the values of those constants change in the future. It will also make your code a bit more readable.