HELP needed with file upload


#1

Can anybody help with this little problem i have. Im pretty new to PHP im trying to develop a file upload site, and have bumped into a problem when trying to apply, I think the end() function; not quite sure for certain. The script isnt complete but in essence at this stage im trying to varify if the file extension the user has choosen is one of the allowed extensions.

Heres the script followed by the error message displayed:

[PHP]<?php require (“design/top.php”);

if (!(isset($_SESSION[‘user’])&& $_SESSION[‘user’]!=’’))
{
header(“Location: loginC2S.php”);
}

?>

boxeD:IN - Upload

<?php if (isset ($_FILES['userfile'])) { $errors = array(); //list of errors $allowed_ext = array('jpg','jpeg','gif','png','mp3'); $file_name = $_FILES['userfile']['name']; $file_ext = strtolower (end(explode ('.', $file_name))); $file_size = $_FILES ['userfile']['size']; $file_tmp = $_FILES ['userfile']['tmp_name']; if (in_array($file_ext, $allowed_ext) ===false) { $errors[] = "Extension not allowed!!!"; } } ?>
Select file to upload:
<?php require ("design/bottom.php"); ?>[/PHP]

Error Message:

The error message is refering to:

[PHP]$file_ext = strtolower (end(explode (’.’, $file_name)));[/PHP]

Any help would be much appreciated. Thanks


#2

You can’t check the end of an execute command, you must check the end of an array…

So, this is bad:
$file_ext = strtolower (end(explode (’.’, $file_name)));

It needs to be something like this:

[php]
$file_ext = strtolower (end($x=explode (’.’, $file_name)));
[/php]
Then, the array is created inside of $x and you look for the end there… Should work…


#3

Thanks ended up resolving the problem using pathinfo:

[php]$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);[/php]

The following also worked great:

[php]$array = explode (’.’, $file_name);
$file_ext = strtolower (end($array)); [/php]