note 84988 deleted from features.file-upload.common-pitfalls by bjori

From: Date: Sat, 09 Aug 2008 17:21:34 +0000
Subject: note 84988 deleted from features.file-upload.common-pitfalls by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143330@lists.php.net to get a copy of this message
Note Submitter: paul at pcserviceselectronics dot co dot uk Reason: useless ---- Spotted whilst trying to test a file upload attachment script that worked on local PHP 5.1.2, but failed on customer's hosting website (PHP 4.1.2). I hunted round and could not find this but $_FILES array on older PHP (4.1.2) did not SET an errors element where as later versions do, even if error is zero. So if you check for errors, (as any good person should do) put a caveat on checking errors to set a flag and to check before any other name/type/size checks.... e.g. $attachment = 0; // ONLY process if attachment actually sent if( isset( $_FILES[ 'attachment' ] ) ) if( empty( $_FILES[ 'attachment' ] ) ) $syserror[] = "Attachment is not valid."; else { if( isset( $_FILES[ 'attachment' ][ 'error' ] ) ) { if( ( $_FILES[ 'attachment' ][ 'error' ] == UPLOAD_ERR_NO_FILE ) ) $syserror[] = "Attachment failed to upload. Contact system administrator"; else if( $_FILES[ 'attachment' ][ 'error' ] > 0 ) $syserror[] = "Attachment upload errored with error = ".$_FILES[ 'attachment' ][ 'error' ]; } // Sometimes PHP 4 servers do not set an errors element so check if errored // but proceed if no errors element if( empty( $syserror ) && empty( $usererror ) ) { $test = $_FILES[ 'attachment' ][ 'size' ]; if( $test == 0 || $test > $_POST[ 'MAX_FILE_SIZE' ] ) $usererror[] = "Attachment size is too large, max size is ".$_POST[ 'MAX_FILE_SIZE' ]." bytes."; else if( preg_match( $ban_ext, $_FILES[ 'attachment' ][ 'name' ] ) > 0 ) $usererror[] = "Attachment name is not valid."; else if( preg_match( $ban_type, $_FILES[ 'attachment' ][ 'type' ] ) > 0 ) $usererror[] = "Attachment type is not valid."; else $attachment = 1; // at this point attachment OK file system wise } } Then in further processing note is $attachment set to 1.. Works well for me NOW!!! -- Paul Carpenter Much receding hairline later

« previous php.notes (#143330) next »