Specific file types upload not working

I am in the process of creating a script where only certains video types can be uploaded but at the moment am stuck.

Here is my code -
[php]

<?php $title = isset($_POST['title']) ? $_POST['title'] : null; $desc = nl2br(isset($_POST['description'])) ? $_POST['description'] : null; $name = isset($_POST['fullname']) ? $_POST['fullname'] : null; $email = isset($_POST['email']) ? $_POST['email'] : null; $country = isset($_POST['country']) ? $_POST['country'] : null; $video = isset($_FILES['video']); $videoname = isset($_FILES['video']['name']); $videotmp = isset($_FILES['video']['tmp_name']); $videosize = isset($_FILES['video']['size']); $videotype = isset($_FILES['video']['type']); $videoacceptable = array( "video/mp4", "video/ogg", "video/quicktime", ); $videopath = "/videos/"; $videofile = $videopath . $video; if(isset($_POST['submit'])) { //ERROR MESSAGES / VALIDATION if(empty($title)) { $errors[] = "A title is required"; echo " #title { background-color:#F5A9A9;border:1px solid #DF0101; } "; } elseif(strlen($title) > 80) { $errors[] = "Your title can only be 80 characters long"; echo " #title { background-color:#F5A9A9;border:1px solid #DF0101; } "; } if(empty($desc)) { $errors[] = "A description is required"; echo " #description { background-color:#F5A9A9;border:1px solid #DF0101; } "; } if(empty($name)) { $errors[] = "Please enter your full name"; echo " #fullname { background-color:#F5A9A9;border:1px solid #DF0101; } "; } elseif(strlen($name) > 32) { $errors[] = "Your name can only be 32 characters long"; echo " #fullname { background-color:#F5A9A9;border:1px solid #DF0101; } "; } if(empty($email)) { $errors[] = "Please enter your email address"; echo " #email { background-color:#F5A9A9;border:1px solid #DF0101; } "; } elseif(strlen($email) > 50) { $errors[] = "Your email addess can only be 50 characters long"; echo " #email { background-color:#F5A9A9;border:1px solid #DF0101; } "; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = "Please enter a valid email address"; echo " #email { background-color:#F5A9A9;border:1px solid #DF0101; } "; } if($videosize = 0) { $errors[] = "You forgot to upload a video"; } elseif($videosize >= 20000000) { $errors[] = "Your video size is too large, 20mb max"; } elseif(!in_array($videotype, $videoacceptable)) { $errors[] = "The file type is not allowed, only allowed .mp4, .ogg and .mov"; } if(count($errors) === 0) { $connect = mysqli_connect("localhost","username","password"); if(!$connect) { header("Location:"); // ADD ERROR LINK } $dbselect = mysqli_select_db("database"); if(!$dbselect) { header("Location:"); // ADD ERROR LINK } $query = mysqli_query("INSERT INTO cover_videos(title, desc, name, email, country, videotmp, videotype, videosize, videopath) VALUES('$title','$desc','$name','$email','$country','$videotmp','$videotype','$videosize','$videopath')"); move_uploaded_file($videotmp, $videofile); //SEND AN EMAIL TO THE USER $to = $email; $subject = "Thank's for your upload"; $message = ' We have received your video

Good News!

We have recieved your video and is awaiting approval.

'; $headers = 'FROM: no-replyk' . "\r\n"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers = 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); header("Location:"); //SUCCESSFUL UPLOAD PAGE } } ?>

[/php]

So whats happening is when i go to upload a file, im uploading a .mov file but the error message that pops up is “The file type is not allowed, only allowed .mp4, .ogg and .mov” but the .mov mime is in the videoacceptable array so im a bit stuck at the moment, any know whats the problem?

Most likely you were given this code or grabbed it off the internet somewhere and you didn’t even look at
the code. The message you are receiving is from this line;

$errors[] = "The file type is not allowed, only allowed .mp4, .ogg and .mov";

So, as you see this error message mentions .mov types. But, that does not mean that your code is set
up for that type, it just means it is mentioned inside the text of the error message. The follow section of
your code sets up the internal values that are valid for your uploads:

$videoacceptable = array(
“video/mp4”,
“video/ogg”,
“video/quicktime”,
);

In this code there is no actual code for generic .mov files. there is one for Quicktime files, but, that is most
likely not an exact match for your video you are trying to upload. Now, how to debug this issue. First, I
would just alter these lines at 60-61 and add some further help to the error message. The old lines:

} elseif(!in_array($videotype, $videoacceptable)) {
$errors[] = “The file type is not allowed, only allowed .mp4, .ogg and .mov”;

Would become this version with the added debugging code displayed:

} elseif(!in_array($videotype, $videoacceptable)) {
$errors[] = "The file type is not allowed, only allowed .mp4, .ogg and .mov
Your file type was: " . $videotype . “
”;

What this will do for you is to show the bad file type and you can just add it to the list of valid ones.
Should help you sort it out!

Oh, also, this is a big issue in larger sites. Error messages should show just enough info for the users
needs not the programmers. Since this is an error message, you could add a logging feature to your site
that would create a log for the webmaster showing that some user attempted to load this file with this
type of data. In that way, you can fix these issues without having to have the user let you know there
was a bad file type they needed uploaded. Hope that makes sense… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service