Jump to content

only allow specific extensions to be uploaded


aebstract

Recommended Posts

The following code is not allowing me to upload any files at all, it returns: Invalid file type.

Any help please?


<?php
session_start();
header("Cache-control: private");
if(!isset($_SESSION["id"]))
{
header("Location: /login/");
}



if(isset($_POST["submit"])) {


$file = $_FILES['userfile'];

$allowedExtensions = array("avi", "mov", "mp4", "mpg", "mpeg", "wmv");

function isAllowedExtension($fileName) {
  global $allowedExtensions;

  return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($file['error'] == UPLOAD_ERR_OK) {
  if(isAllowedExtension($file['name'])) {
    if (move_uploaded_file ($_FILES['file']['tmp_name'], "upload/{$_FILES['file']['name']}")) {
echo "Has been uploaded";
} else {

echo "Not uploaded:<br />";

print "$_FILES[file][error]";


}

  } else {
    echo "Invalid file type";
  }
} else die("Cannot upload");





}





if ($var1 == post){
$content .= '
<form action="/videos/post/" method="post"
enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="62914560" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Post Video" />
</form>
';
}



?>

//return in_array(end(explode(".", $fileName)), $allowedExtensions);
//break this line to debug..

//is it exploding as expexted?
print_r(explode(".", $fileName));

//is it returning extension properly
echo end(explode(".", $fileName);

//check case as in_array() is case sensitive..you can try to lowercase ur extension before comparing

return in_array(end(explode(".", $fileName)), $allowedExtensions);

Okay I threw that in there, sorry it took so long, I was out of work yesterday. Anyways, here is what I have now:

 


<?php
session_start();
header("Cache-control: private");
if(!isset($_SESSION["id"]))
{
header("Location: /login/");
}



if(isset($_POST["submit"])) {


$file = $_FILES['userfile'];

$allowedExtensions = array("avi", "mov", "mp4", "mpg", "mpeg", "wmv");

function isAllowedExtension($fileName) {
  global $allowedExtensions;


  return in_array(end(explode(".", $fileName)), $allowedExtensions);
  print_r(explode(".", $fileName));
  echo end(explode(".", $fileName));
  return in_array(end(explode(".", $fileName)), $allowedExtensions);

}

if($file['error'] == UPLOAD_ERR_OK) {
  if(isAllowedExtension($file['name'])) {
    if (move_uploaded_file ($_FILES['file']['tmp_name'], "upload/{$_FILES['file']['name']}")) {
echo "Has been uploaded";
} else {

echo "Not uploaded:<br />";

print "$_FILES[file][error]";


}

  } else {
    echo "Invalid file type";
  }
} else die("Cannot upload");





}





if ($var1 == post){
$content .= '
<form action="/videos/post/" method="post"
enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="62914560" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Post Video" />
</form>
';
}



?>

 

Displaying just one error : Invalid file type 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.