Using fopen on filenames with embedded spaces
Posted on 2011-05-09
I have an application where I upload a user's file and store the filename, size & type in a mySQL database. The file itself is stored on disk with the filename intact. This takes place on a hosted server (Linux). I can then download the file again, no problems with this.
But, now I am attempting to open the file using fopen. Works fine except when there is an embedded space in the filename. I can't for the life of me figure out how to do this in PHP.
$userFile = "/home/xxx/yyy/" . $data['filename'];
$fileHandle = fopen($userDoc, "r");
if filename includes a space I get:
Warning: fopen(/home/xxx/yyy/User file.doc) [function.fopen]: failed to open stream: No such file or directory in /home/.../ats_functions.php on line 51
This does NOT work:
$userFile = "/home/xxx/yyy/" . str_replace(" ","%20",$data['Resume']);
urldecode / urlencode does NOT work.
file_exists() works OK, so clearly that function can deal with embedded spaces. It's just that fopen() does not work.