简体   繁体   English

如何使用特殊的 unicode 字符在 PHP 中复制文件?

[英]How can I copy files in PHP with special unicode characters?

I have to copy some pdf files from a web to the file path of the new version of the web.我必须将一些pdf文件从网络复制到新版本网络的文件路径。 That pdf files are linked by html a in content.该 pdf 文件由内容中的 html a链接。

So what I'm doing is getting all links to pdf with a regex , copying them to a new path and changing the link to the new path.所以我正在做的是使用正则表达式获取 pdf 的所有链接,将它们复制到新路径并将链接更改为新路径。 But I have problems with some files with special characters.但是我对一些带有特殊字符的文件有问题。 For example:例如:

$original_path = '/path/Dossier%20Nicola%CC%81s%20Combarro_GAL_CAST.pdf'
$decoded_path = urldecode($oiginal_path);
copy($decoded_path, $new_path);

And it shows the following error:它显示以下错误:

File '/path/Dossier Nicola´s Combarro_GAL_CAST.pdf' could not be copied because it does not exist.

Of course it exists but with the name: /path/Dossier Nicolás Combarro_GAL_CAST.pdf .当然它存在,但名称为: /path/Dossier Nicolás Combarro_GAL_CAST.pdf I can open it with a pdf reader, and also I can open it via web browser when I click in the link.我可以用pdf阅读器打开它,也可以在我点击链接时通过网络浏览器打开它。

It seems that PHP decoded a%CC%81 as but Windows decoded it as á .似乎 PHP 将a%CC%81解码为但 Windows 将其解码为á How can copy this file properly?如何正确复制此文件?

I'm running PHP on a VM with Debian, but files are in a shared folder (NFS) with Windows 10 (NTFS).我在带有 Debian 的 VM 上运行 PHP,但文件位于 Windows 10 (NTFS) 的共享文件夹 (NFS) 中。

Windows uses the Windows-1252 character set for file names and paths. Windows 使用 Windows-1252 字符集作为文件名和路径。 If you are working with UTF-8, you will need to convert the file name.如果您使用 UTF-8,则需要转换文件名。

Example:例子:

$utf8FileName = 'René.txt';
$windowFileName = mb_convert_encoding($utf8FileName,'Windows-1252','UTF8');

//Filesystem Win10
$content = file_get_contents($windowFileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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