note 36689 deleted from function.move-uploaded-file by nlopess

From: Date: Wed, 05 May 2004 15:49:21 +0000
Subject: note 36689 deleted from function.move-uploaded-file by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69250@lists.php.net to get a copy of this message
Note Submitter: webmaster@kobrasrealm.com ---- Here is a function that checks the file extension of an uploaded file: <?php function checkext($file,$search) { // Looks for $search at the end of the file $lastpos = strlen($file) - 1; $diffrence = strlen($search)-1; $firstpos = $lastpos - $diffrence; $z = 0; $x = $firstpos; while($z <= $diffrence) { if($file[$x] == $search[$z]) { // Yes $return = 1; } else { // No! $return = 0; } $x++; $z++; } return $return; } ?> now all you have to do is add <?php $file = $_FILES['userfile']['name']; $true = checkext($file,"zip"); echo("It returned $true"); ?> this is helpful if you only want a certain filetype (e.g. ZIP Archives) uploaded onto your website. You could add <?php if($true == 1) { move_uploaded_files($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name']); chmod($_FILES['userfile']['name'],0644); } ?> and you have a great and secure upload script using move_uploaded_file()!

« previous php.notes (#69250) next »