If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

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!

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.

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.

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?

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