I have PHP script to create JPG thumbnails from PDF files. It works untill it encounter *bad* name files with many spaces. (like " aa bb cc    dd.pdf").

Here my simplified code:

```
$cdir = scandir($dir);
foreach ($cdir as $key => $value) 
{ 
  $cfile = pathinfo($dir . DIRECTORY_SEPARATOR . $value);    
   if(strtolower($cfile['extension']) == "pdf")
    {
    $im = new imagick($dir . DIRECTORY_SEPARATOR . $value[0]); //zero is the first page of the PDF
    $im->setImageFormat('jpg');
    $im->thumbnailimage(80, 160, true); // width and height
    $im->writeimage('thumbs/'.$cfile['filename'].'.jpg');           
}}

```

I tried everything I knew: 

* escaping spaces with \
* using \_\_DIR\_\_
* $\_SERVER['SCRIPT\_FILENAME']

I always get or "file not found" or "it's a directory".

I can't rename the PDF files, so how can solve this problem?