Link to home
Start Free TrialLog in
Avatar of jimdgar2
jimdgar2Flag for United States of America

asked on

Using fopen on filenames with embedded spaces

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.

Any ideas?
 
ASKER CERTIFIED SOLUTION
Avatar of Tyler Laczko
Tyler Laczko
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimdgar2

ASKER

professionalcomputersolutions:

Nope, that's not it, now even filenames without spaces can't be found.
Can you paste all your code.

Are you sure that your file_exists and fopen are calling the same location
If you upload the file and that works, generate a fabricated name, perhaps the md5() string of the file name and the current DATETIME.  Store the file on your server with that name and store a translation table in your data base that enables you to find the original name and the fabricated name.  Then when you want the file, you can have a one-line function like get_translated_file_name() to return the values you need.
Ray_Paseur:

Your idea won't work for me. These files (thousands of them) are already stored. I can download and open the files with no problem (I am using a variation of the code found in the example here: http://php.net/manual/en/function.readfile.php).

My problem is simply that fopen doesn't work.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray:

Good advice; I may just do that.

Anyway, problem solved. It was not embedded spaces that were the issue, it was special characters (such as ', #, etc.). By escaping these chars it is now working.

My apologies for wasting anyone's time.