您好我正在尝试创建一个适用于移动设备的简单多文件上传器。现在我在iOS 8上测试。
当我从iPhone 6 Plus上传文件时,我发现脚本在提交时没有从表单中获取文件名。即使在上传png时,它也始终默认为image.jpg。我一直在研究File API,但无法找到我正在寻找的东西。我能够更改文件名,以便所有文件上传而不互相替换,但我想至少保留文件名。有什么帮助吗?
以下是代码的内容。
的index.php
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Upload with PHP</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" accept="image/*">
<input type="submit" value="Upload!" />
</form>
</body>
</html>
upload.php的
<?php
$valid_formats = array("jpg", "png");
$max_file_size = 1024*5000;
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$count.'-'.$name ))
$count++; // Number of successfully uploaded file
}
}
}
}
?>