简体   繁体   中英

Special characters in file name - download vs delete

I am trying to create a file management system on my website. Problem is with downloading files that contain special characters (other work correctly). If I use file_exists($mypath) the result is true therefore file exists.

When deleting this file with unlink($mypath) it also works fine. Only thing that doesn´t work is downloading the file. The download is done via href link where I echo the path but it somehow converts the characters so the link doesn´t work. The solution is in some conversion but I had no success yet.

I suspect that php is converting the special characters into html entities.

You should use the 'rawurlencode' php method to keep the special characters.

The following link talks about you issue (special chars appearing in file name and wanting to create a link):

http://www.dxsdata.com/2015/03/html-php-mailto-content-link-with-special-characters/

Their solution shows the use of rawurlencode, the following was copied from above link just incase the link goes dead:

Snip start...

Scenario

On your website, you want to offer a link which opens a mail client like Outlook with mail and content suggestion. The content contains a link to a file with special characters in its name, which causes Outlook to break the link, eg if it contains spaces or german Umlaute.

Solution

Using PHP, write

<?php
$fullPath = $yourAbsoluteHttpPath . "/" . rawurlencode(rawurlencode($filename));

$mailto = "mailto:a@b.com?subject=File for you&body=Your file: ".$fullPath;

?>

<a href="<?=$mailto?>">Generate Mail</a>

Note the double usage of “rawurlencode” function. The first one is needed for HTML output, the second one will be needed for the part when Outlook takes the link code into its mail window.

Snip end ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM