我复制了一个我已编写过的脚本,但是现在它似乎跳过了if语句,它应该处理上传本身。
我这样做了,实际上有一个文件附加到$files
:
$files = isset($_FILES['files']) ? $_FILES['files'] : false;
$_FILES['files']['tmp_name'][0] = isset($_FILES['files']['tmp_name'][0]) ? $_FILES['files']['tmp_name'][0] : false;
var_dump($files);
var_dump($_FILES['tmp_name']);
表单的其余部分工作得很好。以下是它似乎跳过的部分:
if ((file_exists($_FILES['files']['tmp_name'][0])) || (is_uploaded_file($_FILES['files']['tmp_name'][0]))) {
for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
$file_name = $_FILES['files']['name'][$i];
$file_tmp = $_FILES['files']['tmp_name'][$i];
$file_size = $_FILES['files']['size'][$i];
$file_error = $_FILES['files']['error'][$i];
if ($file_error === 0) {
$directory = 'lib/files/uploads/tasks/projects/'. strtolower($project_id) .'';
$full_directory = $directory . '/' . $file_name;
$counter = 1;
while ($file_exists($full_directory)) {
$info = pathinfo($full_directory);
$filename = $info['dirname'] . '/' . $file_name . '(' . $counter++ . ')' . '.' . $info['extension'];
}
echo $filename;
die();
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
move_uploaded_file($file_tmp, $filename);
$file_path = $filename;
$query4 = $db->prepare("INSERT INTO `projects_tasks_files` (`name`, `path`, `task_id`, `created_at`, `created_by`) VALUES (:name, :pathname, :task_id, :created, :creator)");
$query4->execute(array(
':name' => $file_name,
':pathname' => $file_path,
':task_id' => $new_task_id,
':created' => date("Y-m-d H:i:s"),
':creator' => $_SESSION['id']
));
}
}
}
表单本身看起来像这样:
<form action="" method="post" enctype="multipart/form-data">
<button class="trigger-upload" type="button" role="link">Browse files...</button>
<input type="file" name="files" id="files" multiple>
<ul class="selected-files"></ul>
</form>
答案 0 :(得分:3)
回答这个问题(根据我最初的评论)
您需要将name="files"
视为数组name="files[]"
,以便PHP迭代多个文件。
错误报告在这里会有所帮助。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:只应在暂存时进行显示错误,而不是生产。