note 84988 added to features.file-upload.common-pitfalls

From: Date: Fri, 08 Aug 2008 15:07:00 +0000
Subject: note 84988 added to features.file-upload.common-pitfalls
Groups: php.notes 
Request: Send a blank email to php-notes+get-143311@lists.php.net to get a copy of this message
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 ---- Server IP: 92.48.74.199 Probable Submitter: 213.165.225.88 ---- Manual Page -- http://www.php.net/manual/en/features.file-upload.common-pitfalls.php Edit -- https://master.php.net/note/edit/84988 Del: integrated -- https://master.php.net/note/delete/84988/integrated Del: useless -- https://master.php.net/note/delete/84988/useless Del: bad code -- https://master.php.net/note/delete/84988/bad+code Del: spam -- https://master.php.net/note/delete/84988/spam Del: non-english -- https://master.php.net/note/delete/84988/non-english Del: in docs -- https://master.php.net/note/delete/84988/in+docs Del: other reasons-- https://master.php.net/note/delete/84988 Reject -- https://master.php.net/note/reject/84988 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143311) next »