apache 删除文件名中的空格apache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1167192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove spaces in file names apache
提问by hnhh
1.Hi, I have a internal use only file upload script that uploads the files to a directory. When I upload something from my computer with a spcace in the name i.e example 1.zip it uploads with a space in the name thus killing the link in a email. Is it possible to make apache remove the space when its uploaded or make it a underscore?
1.嗨,我有一个仅供内部使用的文件上传脚本,可将文件上传到目录。当我从我的计算机上传名称中带有空格的内容时,例如示例 1.zip,它上传的名称中带有一个空格,从而取消了电子邮件中的链接。是否可以让 apache 在上传时删除空间或使其成为下划线?
The second problem I am having is how would I parse this to make the link an email link with the url of the file as the body of the email amd the email addy anything?
我遇到的第二个问题是我将如何解析它以使链接成为电子邮件链接,并将文件的 url 作为电子邮件的正文和电子邮件添加任何内容?
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $_FILES['file']['name'])) {
// uploaded file was moved and renamed succesfuly. Display a message.
echo "Link: " . "http://example.org/" . $_FILES["file"]["name"];
回答by soulmerge
You just need to urlencode()your file name and everything is fine:
你只需要urlencode()你的文件名,一切都很好:
echo "Link: http://example.org/" . urlencode($_FILES["file"]["name"]);
But if you want to remove the spaces for another reason, you can use str_replace():
但是,如果您出于其他原因想删除空格,则可以使用str_replace():
$replaced_name = str_replace(' ', '_', $_FILES["file"]["name"]);
rename($uploaddir . '/' . $_FILES['file']['name'], $uploaddir . '/' . $replaced_name);
# You should urlencode() it nonetheless:
echo "Link: http://example.org/" . urlencode($replaced_name);
回答by Nathan
Try:
尝试:
$filename = $_FILES['file']['name'];
$filename = preg_replace("/[^a-zA-Z0-9]/", "", $filename);
//then
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename)) {
// uploaded file was moved and renamed succesfuly. Display a message.
echo "Link: " . "http://example.org/" . $filename;
回答by Pascal MARTIN
As a side note : with the code you are using, what is happening if two files with the same name are uploaded ? If you don't do a check (like "is there a file that already has that name in $uploaddir?") the second file will replace the first one.
附带说明:使用您正在使用的代码,如果上传两个同名文件会发生什么?如果您不进行检查(例如“是否有一个文件已经具有该名称$uploaddir?”),则第二个文件将替换第一个文件。
That might not be something you want... is it ?
那可能不是您想要的……是吗?
If not, to solve that (potential) problem, one solution is to always rename uploaded files, with names youcontrol. (A simple counter would probably to the trick)
如果没有,要解决该(潜在)问题,一种解决方案是始终使用您控制的名称重命名上传的文件。(一个简单的计数器可能会奏效)
Another thing is : $_FILES["file"]["name"]is sent by the client, and, as such, can probably be forged to contains whatever someone would want. If it contains something like "../../index.php" (or something like this - you get the idea), this could allow someone to put any file they want on your server.
另一件事是 :$_FILES["file"]["name"]由客户端发送,因此,可能会被伪造以包含某人想要的任何内容。如果它包含类似“ ../../index.php”的内容(或类似的内容 - 您明白了),这可能允许某人将他们想要的任何文件放在您的服务器上。
To prevent this from happening, you shoud be sure the file name/path used as destination of move_uploaded_filedoes not contain anything "dangerous". A solution could be to use basename. (see, for instance, example #2 on POST method uploads)
为了防止这种情况发生,您应该确保用作目标的文件名/路径 move_uploaded_file不包含任何“危险”内容。一个解决方案可能是使用basename. (例如,参见POST 方法上传的示例 #2 )
You might also want to check the mimetype of the uploaded file, so you don't get executables, for instance -- and you should make sure files uploaded are not executable by the webserver.
例如,您可能还想检查上传文件的 mimetype,因此您不会获得可执行文件——并且您应该确保上传的文件不能被网络服务器执行。

