PHP 8 on Ubuntu.
I’m trying to write a script that will go into a number of subdirectories and look for a .mp4
file, where I can then run some other code with exec()
to create thumbnail images of the videos.
The problem begins where if I do something like this:
$directory = '.'; // This script lives in the same directory where the sub-directories are
$directories = glob($directory . '/*', GLOB_ONLYDIR);
foreach ($directories as $dir) {
echo "Directory: $dir\n";
}
I might get the following output:
Directory: ./Are you ready for something
Directory: ./Yet another folder name
Directory: ./And one more
All of these directories have spaces in them. If I was then to try and cd
into one of those directories:
foreach ($directories as $dir) {
echo "Directory: $dir\n";
exec('cd ' . $dir . "https://stackoverflow.com/");
}
It will error with
cd: ./Are: No such file or directory
This is because to cd
into that directory I would actually need to do something like this, which adds backslashes where the spaces appear in the directory name
cd Are\ you\ ready\ for\ something\/
The output from glob
doesn’t contain those backslashes so can’t be used directly. Is there any way around this?