www.webdeveloper.com
Results 1 to 10 of 10

Thread: Passing file names with spaces in them

  1. #1
    Join Date
    Mar 2013
    Location
    Mankato, MN, USA
    Posts
    6

    Passing file names with spaces in them

    I know nothing of PHP whatsoever. I'm attempting to use a script I obtained here on my Web site. It's not working quite the way I'd like it to.

    In this discussion a PHP script was provided that forces downloads to occur rather than allowing the browser to open the file.

    I'm happy with the script, but I like my files to have spaces in their names. When a client downloads a file, it will then have a more common "2013-04-01 deposition of Jack Sprat.pdf" name rather than "2013-04-01_deposition_of_Jack_Sprat.pdf." It's simply more natural to my end users to see the spaces.

    Is there a way to modify the script to accept file names with spaces?

    The script accepts file names with spaces if I include the download script in the same folder as the HTML and the file to be downloaded.

    However, if the script is in a scripts folder, separate from the HTML and downloadable file, it cannot parse file names with spaces in them; the user downloads an error message. I can post that if that helps; I also don't want to overwhelm.

    Or if the script and HTML are in a parent folder, with the downloadable file in a subfolder, the same error occurs if the file includes a space in its name.

    Is there a way I can force download (rather than display) of files, using a single script called from any other page on my site, and still be able to distribute files with spaces in their names?

    This is the PHP script I'm currently using:
    PHP Code:
    <?php
    $file 
    $_GET['file'];
    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename=".$file.";");
    header("Content-Length: ".filesize($file));
    readfile($file);
    exit;
    ?>
    Any help you're willing and able to offer will be gratefully accepted.

    -- Timothy J. McGowan

  2. #2
    Join Date
    Jul 2010
    Posts
    11
    Hopefully I understood your question correctly, try this:

    PHP Code:
    <?php

    $file 
    $_GET['file']; 
    $file preg_replace('/\s+/''_'$file);
    header ("Content-type: octet/stream"); 
    header ("Content-disposition: attachment; filename=".$file.";"); 
    header("Content-Length: ".filesize($file)); 
    readfile($file); 
    exit; 

    ?>
    Hope this helps!

  3. #3
    Join Date
    Mar 2013
    Location
    Mankato, MN, USA
    Posts
    6
    ajm61894:

    I'm sorry I missed this, somehow. My deepest apologies!

    It appears to me that this script will rename the file, either on the server or as it's being downloaded, replacing spaces with underscores. That is, it looks to me like a file named "One, Two, Three, Four.xyz" will become "One,_Two,_Three,_Four.xyz" -- right?

    That's not what I'm looking for in this circumstance. I want my clients to be able to click on "One Two.mp3" or "Three Four.mpg" and have it download and save with the same name.

    I'd like it to work regardless of Web browser they use -- although I can understand that might be difficult. But at least the big four: IE, FF, Chrome, and Safari/iOS.

    I'd prefer to be able to keep the script in a single directory and call it in html; I really don't want to copy the script into every directory for each file, if I can avoid it.

    Again, I apologize for not noticing the e-mail I received. Cleaning out my in-box today, I finally saw it. Guess it's been more hectic around here than I realized!

  4. #4
    Join Date
    Mar 2009
    Posts
    452
    what file names are actually stored in directory, also all files are stored in same directory or different ? what your link's href looks like ?

  5. #5
    Join Date
    Mar 2013
    Location
    Mankato, MN, USA
    Posts
    6
    ZABI:

    I record and transcribe meetings. Each meeting goes in a separate directory.

    For what it's worth, access to the directories is password protected and I have SSL encryption enabled.

    I include a PDF of the transcript and sometimes an audio file, depending on the client.

    So my downloads folder and the files within it might be arranged thus:
    /download/SmithV.Jones/2013-04-05 Fred Jones deposition.pdf
    /download/SmithV.Jones/2013-04-05 Fred Jones deposition.mp3
    /download/SmithV.Jones/2013-04-05 Susan Smith deposition.pdf
    /download/SmithV.Jones/2013-04-05 Susan Smith deposition.mp3
    /download/DoeRey/2013-04-10 Mi Fah statement under oath.pdf
    /download/DoeRey/2013-04-10 Mi Fah statement under oath.mp3

    The names change for each meeting and for each witness or proceeding. The files and directories remain on my site for a week or so and are replaced by new files in directories with new names.

    The type of link I'd like to use is one that changes only the file name in the index.html file in the directory. For instance, in /download/SmithV.Jones/index.html I'd like to have a link that works something like this:
    <a href="/zBatch/direct_download.php?file=2013-04-05 Fred Jones deposition.pdf">
    or
    <a href="/zBatch/direct_download.php?file=/download/SmithV.Jones/2013-04-05 Fred Jones deposition.pdf">

    That way I could just customize the link and insert it into my boilerplate index file.

    I want the user to end up with "2013-04-05 Fred Jones deposition.pdf" on their hard drive, not _download_SmithV.Jones_2013-04-05_Fred_Jones_deposition.pdf.

    I'm presently using a directive in .htaccess to force download of all pdf and mp3 files, which works perfectly -- except browsers such as Chrome and Firefox work too smoothly and it's not immediately obvious that you've downloaded anything. Nothing appears to happen except a brief flash of a downward-pointing arrow.

    So while that works, and while in most cases the people who are using those browsers understand what's going on or are smart enough to grasp it fairly quickly, it's not the user experience I'd prefer. I'd rather have a View (or Listen) link alongside a Download link so that the user can anticipate what's going to happen when they click.

    For what it's worth, .htaccess looks like the following:
    <FilesMatch "\.(mp3|pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
    </FilesMatch>

    I've done enough macro programming to understand that I probably need to get some quotation marks around the file name, but I'm unable to figure out how -- nor am I entirely sure that's the whole problem.

    All help from anyone gratefully accepted!

    -- Tim

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,355
    Maybe this?
    PHP Code:
    header ("Content-disposition: attachment; filename=".urlencode($file).";"); 
    If that doesn't work, next thing I'd try:
    PHP Code:
    header ("Content-disposition: attachment; filename=".addcslashes($file' ').";"); 
    If that doesn't work either, then:
    PHP Code:
    header ("Content-disposition: attachment; filename=".urlencode(addcslashes($file' ')).";"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Mar 2013
    Location
    Mankato, MN, USA
    Posts
    6
    NonDog:

    No help, I'm afraid, not in Firefox, anyway.

    The first line offered to download a file with the spaces replaced with plus signs:
    Test+download+file.pdf

    The second line of code cut the name off after the first word, added an underscore, and ignored the file extension; I downloaded:
    Test_

    The third line offered to save a file I was afraid to have touch a Windows hard drive, because files with percent signs in them can be difficult to handle:
    Test%5C+download%5C+file.pdf

    In each case, even in the two times that the file reflected the pdf extension, the Firefox Save As dialog advised me that the file was a JPG, for what that's worth.

    Any other thoughts?

    -- Tim
    (My favorite Pratchett line: "The Truth Shall Make Ye Fret." So fretfully true!)

  8. #8
    Join Date
    Mar 2009
    Posts
    452
    what i have understood so far is: you want to save files to be downloaded as they are on server. try this

    Code:
    $file = $_GET['file'];  
    header('Content-Description: File Transfer');
    header('Content-Type:application/octet-stream');
    header('Content-Transfer-Encoding: Binary'); 
    header("Content-disposition: attachment; filename=\"".basename($file)."\"");
    header('Content-Length: ' . filesize($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    readfile($file);
    exit;
    hope it works for you

  9. #9
    Join Date
    Mar 2013
    Location
    Mankato, MN, USA
    Posts
    6
    ZABI:

    Works perfectly in up-to-date versions of Internet Explorer 10, Internet Explorer 8, Firefox, and Chrome. Thank you so very much!

    I wonder whether you can help me tweak it just a bit further: I'd like to not have to remember to copy the script into the download directory each time.

    Instead, I'd like to edit a boilerplate index file that would include a reference to /scripts/force_download.php. I could then change the name of the file to be downloaded in the boilerplate and not have to remember to upload or copy the script into the directory.

    I've seen ways on the Web to get php to run in an html file, and that looks like it's more work that it's worth to me. I would certainly be able to modify my main .htaccess file, and perhaps I can tinker with that when I have more time and not annoy anyone else here.

    I'm using this for a link in a test page:
    Download <a href="/zBatch/forcedown.php?file=https://mcgowanreporting.com/clients/Testing/How to use a PDF portfolio.pdf">How to use a PDF portfolio.pdf</a>

    When I use that, I download an error message followed by the contents of Testing/index.html. The error message reads:
    <br />
    <b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for https://mcgowanreporting.com/clients/Testing/How to use a PDF portfolio.pdf in <b>/home/[user]/public_html/zBatch/forcedown.php</b> on line <b>8</b><br />
    <br />
    <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/[user]/public_html/zBatch/forcedown.php:8) in <b>/home/[user]/public_html/zBatch/forcedown.php</b> on line <b>8</b><br />
    <br />
    <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/[user]/public_html/zBatch/forcedown.php:8) in <b>/home/[user]/public_html/zBatch/forcedown.php</b> on line <b>9</b><br />
    <br />
    <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/[user]/public_html/zBatch/forcedown.php:8) in <b>/home/[user]/public_html/zBatch/forcedown.php</b> on line <b>10</b><br />

    At present, on https://mcgowanreporting.com/clients/Testing/ is an index.html page with a number of links. The first opens a PDF in the browser. The second links saves the file to the user's hard drive properly, thanks to you, ZABI. The third saves the error message and text of the index.html file as described immediately above. The others work as described next to them.

    I can train myself like any monkey to add the script file to each directory as I create them; so go ahead and call me lazy for wanting to reduce the chances for errors.

    But I'll accept input from anyone:
    Is there a way to call the script from a single directory?
    Or would I be better served reconfiguring my site and taking the time to learn how to insert php scripts into the boilerplate html?
    Or might something else work better in this situation? (Yeah, hire someone competent to manage my site for me, I know -- and if I were busier, I sure would!)

    Thanks so much, ZABI, for your excellent work. How may I acknowledge you in the script? Do you have a profile here or a Web page I can insert into a comment in the script?

  10. #10
    Join Date
    Mar 2009
    Posts
    452
    its my pleasure to help others,

    you can pass file name wtih directory name, for example: <a href="/zBatch/forcedown.php?file=clients/Testing/How to use a PDF portfolio.pdf">How to use a PDF portfolio.pdf</a> (a relative path not url), this way you will not require to put this script in all directories.


    as you said you have access to modify your .htaccess, why didn't you try with it,

    just put the following in your .htaccess; it should save you from using that script

    Code:
    AddType application/octet-stream .zip
    AddType application/octet-stream .pdf
    AddType application/octet-stream .mp3
    # you can add more types just like above

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles