note 36689 added to function.move-uploaded-file
| From: | webmaster at kobrasrealm dot com | Date: | Sat, 18 Oct 2003 21:34:03 +0000 |
| Subject: | note 36689 added to function.move-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58859@lists.php.net to get a copy of this message | ||
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()!
----
Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36689
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36689&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36689&report=yes
Search -- http://master.php.net/manage/user-notes.php