Home >Backend Development >PHP Tutorial >上传中文文件名为空

上传中文文件名为空

WBOY
WBOYOriginal
2016-06-23 14:18:41938browse

比如 你好.txt 上传到服务器就成了 .txt,文件名部分是空的,不知道为什么。windows系统下文件名不是不允许为空吗?

上传处理代码

<?$upload_path = $_SERVER['DOCUMENT_ROOT']."/upload/";$tmp_file = $_FILES['myfile']['name'];$dest_file = $upload_path.basename($tmp_file);if(move_uploaded_file($_FILES['myfile']['tmp_name'],$dest_file)){ echo "文件已上传至服务器根目录的upload目录下!";	 print_r($dest_file);}else{  echo	"文件上传时发生了一个错误".$_FILES['myfile']['error'];}?>


HTML代码
<html><head><title>upload</title></head><body><form enctype="multipart/form-data" action="upload.php" method="POST">上传此文件:<input name = "myfile" type="file" /><input type="submit" value="提交上传" /></form></body></html>


回复讨论(解决方案)

如果文件名是英文或者数字都是没有问题的。

建议用英文、数字作为文件名,这样移植行更强一些。

如果非要中文名,那就urlencode一下,读取时urldecode过来

gbk字符在utf-8环境中显示为空的正常现象

如果用户上传自己的文件中带有中文怎么办

urlencode和urldecode应该怎么写代码呀,小弟学PHP时间不长,请各位大神指点。

用当前时间戳命名最简单
$dest_file = $upload_path.basename($tmp_file);
改为

$ext=pathinfo($tmp_file,PATHINFO_EXTENSION)$dest_file = $upload_path.time().'.'.$ext;


或者用base64编码,感觉要比urlencode要好。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn