Results 1 to 10 of 10

Thread: Passing file names with spaces in them

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Mankato, MN, USA

    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:
    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename=".$file.";");
    header("Content-Length: ".filesize($file));
    Any help you're willing and able to offer will be gratefully accepted.

    -- Timothy J. McGowan

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

    PHP Code:

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

    Hope this helps!

  3. #3
    Join Date
    Mar 2013
    Mankato, MN, USA

    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
    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
    Mankato, MN, USA

    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">
    <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

    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
    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' ')).";"); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Mar 2013
    Mankato, MN, USA

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

    The first line offered to download a file with the spaces replaced with plus signs:

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

    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:

    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
    what i have understood so far is: you want to save files to be downloaded as they are on server. try this

    $file = $_GET['file'];  
    header('Content-Description: File Transfer');
    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');
    hope it works for you

  9. #9
    Join Date
    Mar 2013
    Mankato, MN, USA

    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
    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

    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11401 seconds
  • Memory Usage 3,241KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (2)bbcode_code
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates