File upload when filename contains multiple dots

I have a video upload page for my customers. When they try to upload a file that has multiple dots in the file name, the file will not upload. I do not want my customers to have to change the file name. I need the periods removed or the file renamed before determining the file extension.

This filename “testfile.mov” will upload but this file “test.file.mov” will not upload.

My code:

$today=date(“Y-m-d”);

$name= $_FILES[‘file’][‘name’];

$tmp_name= $_FILES[‘file’][‘tmp_name’];

$position= strpos($name, “.”);

$fileextension= substr($name, $position + 1);

$fileextension= strtolower($fileextension);

$newfilename = $lastname.’-’ .$today.’.’.$fileextension;

if (isset($name)) {

$path= ‘uploads/videos/’;
if (empty($name))
{
echo “Please choose a file”;
}
else if (!empty($name)){
if (($fileextension !== “mp4”) && ($fileextension !== “ogg”) && ($fileextension !== “webm”) && ($fileextension !== “mov”))
{
echo “The file extension must be .mp4, .ogg, .webm, or .mov in order to be uploaded”;
}

else if (($fileextension == “mp4”) || ($fileextension == “ogg”) || ($fileextension == “webm”) || ($fileextension == “mov”))
{
if (move_uploaded_file($tmp_name, $path.$newfilename)) {
echo “Video uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}
}

Think a little… What does that line do? It finds the FIRST occurrence of a period. Incorrect!
You should change that to find the last one. Like this…

$position = strrpos($name, ".");

This will find the first period from the right to left instead of the first from left to right.

Bet that will fix this issue!

Yet, that would be the “correct” thing to happen. You shouldn’t be catering your code to make up for a users careless mistake. You will just be kicking the can (problem) down the road to rear it’s head somewhere else.

If this is a regular problem (Never heard of it in 30+ years) you could always offer a suggestion to the user on a failed upload to check for a proper file name.

I have seen this issue before. Just make the change I gave you and it will work.
You just are not locating the correct period for the extension. My version will solve that.

mburrows, did you try it? Did it work for you?

Sponsor our Newsletter | Privacy Policy | Terms of Service