Link to home
Start Free TrialLog in
Avatar of pteeter
pteeter

asked on

best way to encode whitespace in URL passed via 'mailto' HREF link

I wrote a PHP based web front end for file upload & download a few years ago.

Outside our firewall, people connect to it via a browser or via WebDAV.  They can download files, navigate thru nested directories, and upload files as well.  Authentication is required to browse client directories, auth is tied to an Open LDAP directory.

Inside of our firewall, people connect to via SMB/CIFS or AFP file sharing.

Our external user population like the convenience of having a direct link to a file for download.

Our internal user population lacks the technical savvy to construct such direct links for delivery via email.

I am trying to code up a link/form that does the following -

1. shows up only for internal web connections (using HTTP_PC_REMOTE_ADDR in $_SERVER allows for this it appears)
2. addresses an email message with a URL to the file in the body of the message

Early tests showed this was quite possible using PHP/HTML.

Further coding to allow for files/folders with whitespace in the name have proved more challenging.

I've tried a variety of methods to accomplish this...

- php eregreplace " " with "%20"
- php urlencode
- now javascript document.write with either escape() or encodeURI()

When the source code for the web page generates, the URL is formatted properly - " " prints out as "%20".  As soon as the mail client launches though, the URL loses the proper encoding.

My next attempt will involve setting the MIME type specifically I think.

The other option is to 'tell' my local users *NOT* to use spaces in file/folder names.  Thing is I've been trying to get cooperation with such a policy for years...sigh.


if (ereg('^192.168.', $_SERVER['HTTP_PC_REMOTE_ADDR']) || $_SERVER['HTTP_PC_REMOTE_ADDR'] == '72.214.10.199') {
     print '<tr>';
     print '<td valign="top" align="right" bgcolor="#999999" colspan="3" class="mail_text">';
     print '<script language="JavaScript" type="text/javascript">';
     print 'document.write("<a href=\"mailto:?subject=file download from vitrorobertson?body=http://ftp.vitrorobertson.com" + encodeURI("'.$_GET['dir_to_list'].'/'.$item.'") + "\">Email direct link to client</a>")';
     print '</script>';
     print '</td>';
     print '</tr>';
};
 
$_GET['dir_to_list'] is, obviously, passed from the last posting of the self-posting page/form.  I am obviously not using CSS for table generation.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of siliconbrit
siliconbrit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ddrudik
pteeter, please show an example of what a generated html table code looks like for your filenames with spaces.
Avatar of pteeter
pteeter

ASKER

The code is attached.

You'll see the inclusion of '%20' strings, substituting for white spaces in the body portion of the mailto URL.

Problem is that when the mail client is launched, the %20 strings disappear.

Hope this is useful.

Thanks for the help.
<tr>
<td valign="top" class="list_text" align="left">
<b><i>file:</i></b>
</td>
<td valign="top" class="list_text" align="left">
<a href="/pickup/94 Shot List Folder.sitx">94 Shot List Folder.sitx</a>
</td>
<td valign="top" class="list_text" align="center">(64.16 mb, 02/08/08)</td>
</tr>
<tr>
<td valign="top" align="right" bgcolor="#999999" colspan="3" class="mail_text">
<a href="mailto:?subject=file download from COMPANY_NAME&body=http://transfer.company_name.com%2Fpickup%2F94%20Shot%20List%20Folder.sitx" type="text/html">Email direct link to client</a>
</td>
</tr>

Open in new window

This seemed to work for me:
<a href="mailto:?subject=file download from COMPANY_NAME&body=<?php echo urlencode("http://transfer.company_name.com/pickup/94 Shot List Folder.sitx"); ?>" type="text/html">

I would recommend not creating HTML entities in the string and then pass it to urlencode as shown.
Avatar of pteeter

ASKER

i'll try it again, urlencode that is.

It didn't seem to cooperate before.

Question - which email client are you passing the URL to as body?

I'm using Outlook 2003 and Entourage 2004/2008.

I feel like neither has cooperated thus far.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pteeter

ASKER

I hope to revisit this soon and try the '+' as spaces encoding method.

Thanks.

Avatar of pteeter

ASKER

no problem, i havent been able to work on the project in some time.