Jump to content

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 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.