PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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):
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
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.