I have this code where I would like to convert a file (using ffmpeg) if the file does not have to correct extension. I’m using preg_match but I’m having problems.
When I manually convert the file it works and it works in php too but when I run this code along with ffmpeg it doesn’t work it doesn’t upload the file nor does it convert it.
<?php // Access the $_FILES global variable for this specific file being uploaded // and create local PHP variables from the $_FILES array of information $fileName = $code_to_execute; // The file name $fileTmpLoc = $code_to_execute; // File in the PHP tmp folder $fileType = $code_to_execute; // The type of file it is $fileSize = $code_to_execute; // File size in bytes
if (preg_match("/.(wmv|avi|flv)$/i", $fileName) ) { shell_exec("ffmpeg -i uploads/".$code_to_execute." uploads/".$code_to_execute.""); // This condition is only if you wish to allow uploading of specific file types unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder exit(); ?>
BUT
when I run this code it uploads the file but doesn’t convert it
<?php // Access the $_FILES global variable for this specific file being uploaded // and create local PHP variables from the $_FILES array of information $fileName = $code_to_execute; // The file name $fileTmpLoc = $code_to_execute; // File in the PHP tmp folder $fileType = $code_to_execute; // The type of file it is $fileSize = $code_to_execute; // File size in bytes
if (!preg_match("/.(wmv|avi|flv)$/i", $fileName) ) { shell_exec("ffmpeg -i uploads/".$code_to_execute." uploads/".$code_to_execute.""); // This condition is only if you wish to allow uploading of specific file types unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder exit(); ?>
There’s literally nothing different with the code except I added a "!" in front of preg_match turning it into !preg_match
What the hell is the difference and why doesn’t it work, someone help me out here T.T?
note the backslash before the dot. The dot means any character - if you don’t add the backslash then file names like videowmv, lavi, ect. will match the regex, too, while I suppose you only want video.vmw, l.avi, etc.
I want it to look for any file that doesn’t have the extensions .gif/png/mp4 and then I want ffmpeg to convert the file so if it’s a jpg it gets converted to png and if it’s an avi flv wmv it gets converted to mp4
I understand that but the conversion doesn’t work. So for example I’m saying if the extension doesn’t match .mp4 then convert but it doesn’t matter whether I use the “!” or not it still won’t convert…anyway around this?
Example (this won’t convert nor will it upload the file):
<?php
// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$fileName = $_FILES["uploaded_file"]["name"]; // The file name
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is
$fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true
else if (!preg_match("/\.(mp4)/i", $fileName) ) {
shell_exec("ffmpeg -i uploads/".$_FILES["uploaded_file"]["type"]." uploads/".$_FILES["uploaded_file"]["type"].".mp4");
// This condition is only if you wish to allow uploading of specific file types
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
}
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
// Check to make sure the move result is true before continuing
if ($moveResult != true) {
echo "ERROR: File not uploaded. Try again.";
exit();
}
}
?>
I know from limited past experience that exec() can be extremely picky
For example purposes only, lets say $_FILES["uploaded_file"]["type"] is “avi”
Substituting that into the command shell_exec("ffmpeg -i uploads/".$_FILES["uploaded_file"]["type"]." uploads/".$_FILES["uploaded_file"]["type"].".mp4");
gives shell_exec("ffmpeg -i uploads/avi uploads/avi.mp4");