Using fopen on filenames with embedded spaces

Posted on 2011-05-09
Last Modified: 2012-06-21
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?
Question by:jimdgar2
    LVL 10

    Accepted Solution

    you need to put apostrophes around the file name.

                  $userFile = "'/home/xxx/yyy/" . $data['filename'] . "'";

    Author Comment


    Nope, that's not it, now even filenames without spaces can't be found.
    LVL 10

    Expert Comment

    by:Tyler Laczko
    Can you paste all your code.

    Are you sure that your file_exists and fopen are calling the same location
    LVL 107

    Expert Comment

    by:Ray Paseur
    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.

    Author Comment


    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:

    My problem is simply that fopen doesn't work.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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

    Author Comment


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now