• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1466
  • Last Modified:

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?
 
0
jimdgar2
Asked:
jimdgar2
  • 3
  • 2
  • 2
2 Solutions
 
Tyler LaczkoCommented:
you need to put apostrophes around the file name.


              $userFile = "'/home/xxx/yyy/" . $data['filename'] . "'";
0
 
jimdgar2Author Commented:
professionalcomputersolutions:

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

Are you sure that your file_exists and fopen are calling the same location
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
Ray PaseurCommented:
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.
0
 
jimdgar2Author Commented:
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.
0
 
Ray PaseurCommented:
If it were hundreds of millions of files, I might see why it would take some special effort to make it work, perhaps taking the site down for a little while.  But with thousands of files you can use ALTER TABLE to add the columns to your data base and in a matter of seconds you can create the new columns with the file names.  Then you can walk the table and rename the files.  Catch the outliers for manual intervention.  Write the scripts so you can restart them if they time out.  It's a one-time fix to the data base and scripts, and a permanent solution after that.

Best of luck with your project, ~Ray
0
 
jimdgar2Author Commented:
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.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now