我一直在试图了解如何做这个多图像上传的事情几天了,我无法弄明白。我是新手,所以请不要吝啬。我在实际上寻求帮助之前检查了几个网站。
这是表格的页面
<?php
require_once("photoalbum-common.php");
require_once "lib/dbutils.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' href='CSS/actors-styles.css'>
<link rel='stylesheet' href='http://www.w3schools.com/lib/w3.css'>
</head>
<body>
<?php
$pdo = connect();
if ( isset( $_POST['formid']) && $_POST['formid'] == 'fileupload') {
$photoid = addPhotograph( $pdo, $_POST['name'], $_POST['description'], 'self', $_FILES['images']);
}
?>
<div class='w3-container w3-center'>
<button onclick="document.getElementById('id01').style.display='block'" class="link">Add images</button>
</div>
<div id="id01" class="w3-modal w3-animate-opacity">
<div class="w3-modal-dialog">
<div class="w3-modal-content w3-card-4 w3-animate-zoom">
<header class="w3-container w3-teal">
<a href="" class="w3-closebtn">×</a>
<h2>Upload photograph</h2>
</header>
<div class="w3-container">
<form action='?' method='post' enctype='multipart/form-data' id='photoform' class='w3-container'>
<div class="w3-group">
<!-- <input type='file' name='uploadfile' class='w3-input'> -->
<label class="w3-label">Image file</label>
<input type="file" name="images[]" multiple class='w3-input'/>
</div>
<div class="w3-group">
<label class="w3-label">Name</label>
<input type='text' name='name' class='w3-input' required>
</div>
<div class="w3-group">
<label class="w3-label">Description</label>
<textarea name='description' class='w3-input' required></textarea>
</div>
<input type='hidden' name='formid' value='fileupload'>
<input type='submit' value='Upload' id='uploadbutton' class='w3-btn'>
</form>
</div>
<footer class="w3-container w3-teal">
<p></p>
</footer>
</div>
</div>
</div>
</body>
</html>
这里有照片photoalbum-common.php
function addPhotograph( $pdo, $name, $description, $user, $fileinfo) {
$stmt = $pdo->prepare("INSERT INTO photographs ( `name`, `description`, `user`) VALUES (?, ?, ?)");
$stmt->execute( array( $name, $description, $user));
$photoid = $pdo->lastInsertId();
$tmp_name = $fileinfo["tmp_name"];
$name = $photoid."_".$fileinfo["name"];
$stmt = $pdo->prepare("UPDATE photographs SET `image`=? WHERE `photoid`=?");
$stmt->execute( array( $name, $photoid));
$result = move_uploaded_file($tmp_name, "images/$name");
return $photoid;
}
function reArrayFiles(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
我从http://php.net/manual/en/features.file-upload.multiple.php得到的第二个功能,不幸的是,我没有设法做任何事情。我能想到的最好的方法是显示两个没有显示实际图像的上传,因为它们没有上传到数据库。
如果有人可以帮助我或指导我如何使用上面的内容上传多个图像,因为当前内容仅适用于一个图像上传。提前谢谢。
答案 0 :(得分:0)
它无效,因为您已设置文件数组并使用单个文件执行代码。
请使用以下代码更正:
if(!empty($fileinfo)){
$total_files = count($fileinfo['name']);
for($i = 0 ; $i<$total_files ; $i++)
{
$tmp_name = $fileinfo["tmp_name"][$i];
$name = rand()."_".$fileinfo["name"][$i];
$result = move_uploaded_file($tmp_name, "images/$name");
}
}
$tmp_name = $fileinfo["tmp_name"];