我正在尝试将新用户的多张照片上传到服务器上的新目录,并根据其名称和ID在数据库中为该人员分配目录名称。新目录的路径将添加到数据库中的字段,以便稍后可以引用这些照片。除此之外,所有其他数据库功能都正常工作。
我这个项目已经工作了6个月,而且这个功能在某个阶段正在发挥作用。我不确定我搞砸了什么。目前我获得'0个文件成功上传',没有创建新目录或引用。数据库连接必须正常,因为此处未包含的代码中的其他信息添加时没有问题。
请帮忙。我正在拉着我留下的小头发!
$count = 0;
$valid_formats = array("jpg", "png");
$max_file_size = 1024*5000;
$lastID = $mysqli->insert_id;
$path = '../img/gallery/'.$lastID.'_'.$displayName.'/';
$path2 = './img/gallery/'.$lastID.'_'.$displayName.'/';
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to execute all files
foreach ($_FILES['photoUploader']['name'] as $f => $name) {
if ($_FILES['photoUploader']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['photoUploader']['error'][$f] == 0) {
if ($_FILES['photoUploader']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($displayName, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else { // No error found!
// Create new directory based on unique ID and display name
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
// move temporary files to permanent location
if(move_uploaded_file($_FILES["photoUploader"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
// add image folder url to db for future use
$imageUrlQuery = "UPDATE models SET photoLocation=? WHERE id=$lastID";
$imageUrlstmt = $mysqli->prepare($imageUrlQuery);
$imageUrlstmt->bind_param('s', $path2);
$imageUrlstmt->execute();
}
}
}
echo $count . " file(s) uploaded successfully!";
答案 0 :(得分:1)
将表单设置为:
multipart/form-data
群集上传项目:照片[]
<input type="file" name="photos[]" multiple="multiple" id="multipic"/>
<label for="multipic"><btn> Select 3 Photos </btn></label>
处理档案
foreach($_FILES['photos']['tmp_name'] as $key => $tmp_name ){
$file_name = $_FILES['photos']['name'][$key];
$file_tmp = $_FILES['photos']['tmp_name'][$key];
$file_size = $_FILES['photos']['size'][$key];
$photo1="dir/where/photos/go/";
$photo1=$photo1 . basename($_FILES['photos']['name'][0]);
$fz1=$_FILES['photos']['size'][0];
if(move_uploaded_file($_FILES['photos']['tmp_name'][0], $photo1)) {
/* do whatever you like here */ }
$photo2 ...
$photo3 ...
}
您可以为每个项目执行此操作:[0] [1] [2]等。
答案 1 :(得分:0)
elseif( ! in_array(pathinfo($displayName, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
$ displayName需要更改为$ name。就是这样。叹息......