PHP - Filename formatting questions with PHP Script

My script works fine when I pass a normal filename like


But when I pass it a name like

code (2).mp4

My script fails.  I tried urlencode wrapped on the variables, but no luck is there another command to use here?

class VideoTile
    public static function createMovieThumb($srcFile, $destFileName)
        // Change the path according to your server.
        $ffmpeg_path = '';

        $output = array();

        $cmd = sprintf('%sffmpeg -i %s -an -ss 00:00:05 -r 1 -vframes 1 -y %s',
            $ffmpeg_path, $srcFile, $destFileName);

        exec($cmd, $output, $retval);

        if ($retval)
            return false;

        return $destFileName;

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Escape any spaces or special characters in your filename. For example:

code\ (2).mp4
dj\ \"randy\"\ biggs.pls

The \ should go before spaces and such.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nathan RileyFounderAuthor Commented:
They are passed via another script.

So the parentheses are not causing the issue then it's the space?

I tried addslashes on the variables, but still didn't work.
Nathan RileyFounderAuthor Commented:
Thanks for help, ended up changing the naming conventions on the uploaded files to avoid this issue all together.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Correct, it's the space in this case. Ultimately, you're running this as a system command, and if you don't escape the space, then the system will see this:

/ffmpeg -i code (2).mp4 -an -ss 00:00:05 -r 1 -vframes 1 -y somefile

That breaks up as:
  -i code                 <-- ffmpeg looks for a file called "code"
  (2).mp4                <--- this parameter doesn't even make sense to ffmpeg
  -an -ss 00:00:05
  -r 1
  -vframes 1
  -y somefile

When you escape the space, the system shell knows that it is a part of the filename, so:

/ffmpeg -i code\ (2).mp4 -an -ss 00:00:05 -r 1 -vframes 1 -y somefile parsed as:
  -i code (2).mp4                <--- look for a file called code (2).mp4
  -an -ss 00:00:05
  -r 1
  -vframes 1
  -y somefile
Nathan RileyFounderAuthor Commented:
Yeah, is there a command to for spaces to be escaped, just for future reference?

In this case it's a file being uploaded then passed to ffmpeg so it's user input, not something I can prevent and is passed as a variable.

One of these perhaps?

addslashes($variableName) or urlencode($variableName)
Just use:

$variableName = str_replace(" ","\\ ",$variableName);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.