Having trouble here understanding preg_match

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?

The ! is a “not” preg_match so using ! you are saying that if it doesn’t match, do something.

Also your regex should look like this:

preg_match("/\.(wmv|avi|flv)$/i", $fileName)

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.

2 Likes

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 if (!preg_match("/\.(mp4)$/i", $fileName)) { shell_exec("ffmpeg -i uploads/".$code_to_execute." uploads/".$code_to_execute.".mp4"); } ?>

Your code examples have $code_to_execute all over the place.

Obviously the code examples are not the real code being used. Can you post it?

1 Like
<?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();
} 
}
?>

Is safe_mode enabled?

http://php.net/manual/en/features.safe-mode.functions.php

shell_exec() (functional equivalent of backticks) This function is disabled when PHP is running in safe mode.

No it’s disabled…I didn’t necessarily have to use shell_exec() I could’ve just used exec() either one doesn’t work.

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");

That doesn’t look right to me.

:open_mouth: I see what you’re saying so would something like this work?

<?php
exec("ffmpeg -i uploads/".$fileName.$fileType." uploads/".$fileName.".mp4");
?> 

Where $fileName = $_FILES["uploaded_file"]["name"];
and $fileType = $_FILES["uploaded_file"]["type"];

Ok so I got this working with this code

<?php
else if (preg_match("/\.(wmv)$/i", $fileType)) {
preg_replace("ffmpeg -i uploads/".$_FILES["uploaded_file"]["type"]." uploads/".$_FILES["uploaded_file"]["type"].".mp4"); 
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
 exit();
}
?>

But now I need it to do another thing. Which is upload that converted file to the database…anyone help?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.