我正在为照片网站构建上传器,大多数上传的照片的类型为1232132_1231231_12.jpg。当我运行pathinfo()时,得到扩展的空白输出。
这是我的代码
$target_dir = "Photos/";
$species = filter_input(INPUT_POST, 'species');
$country = filter_input(INPUT_POST, 'country');
$type= filter_input(INPUT_POST, 'type');
include_once 'connection.php';
echo $target_dir . basename($_FILES["file"]["name"]);
for($a=0;$a<count($_FILES['file']['tmp_name']);$a++)
{
$target_file = $target_dir . basename($_FILES["file"]["name"][$a]);
$imageFileType=pathinfo($target_file, PATHINFO_EXTENSION);
var_dump(pathinfo($target_file));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo "<br> Sorry, only JPG, JPEG, PNG & GIF files are allowed. your file is a $imageFileType <br>";
}
}
Run Code Online (Sandbox Code Playgroud)
照片/ 2014-02-21 18.19.08.jpg
echo $target-file;
Run Code Online (Sandbox Code Playgroud)
给出此输出Photos / 2014-02-21 18.19.08.jpg
对于var_dump,它被回显到屏幕上,array(3){[“ dirname”] => string(6)“ Photos” [“ basename”] => string(1)“ 2” [“ filename”] => string(1)“ 2”}
我的代码是否有问题,或者pathinfo()与数字和特殊字符文件名不能很好地配合使用?
是否有另一个函数可以处理这些类型的文件名,或者应该用(。)炸开$ target_file。并采取返回数组中的最后一个元素?
问题不是路径信息。你在做一些奇怪的事情:
$target_file = $target_dir . basename($_FILES["file"]["name"][$a]);
Run Code Online (Sandbox Code Playgroud)
该行处于循环中,其中 $a 介于 0 和... $_FILES["file"]["name"] 之间返回文件名。但:
$_FILES["file"]["name"][0]
Run Code Online (Sandbox Code Playgroud)
将返回文件名的第一个字母(例如 2)。因此,您正在对变量执行路径信息:
$target_file = $target_dir . basename($_FILES["file"]["name"][$a]); // looks like $target_file = Photos/2 (when $a = 0)
Run Code Online (Sandbox Code Playgroud)
像这样更改该行:
$target_file = $target_dir . basename($_FILES["file"]["name"]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2660 次 |
| 最近记录: |