<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="image" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="image" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
这是我的表格,它有两个带有一个按钮的上传图像我想同时上传两张图像并将它们放在 datase 中的两个单独的列中,因为我将在滑块中使用这两张图像,一个用于大图像,另一个用于缩略图我尝试只上传一张图像,这很好,但是您如何同时上传两张图像这是我的代码用于添加图像。此代码仅上传一张图片,如何将其更改为上传两张?
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["image"]['tmp_name'])){
$folder = "images/imagegallery/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl) VALUES (?)");
$stmt->bindValue(1,$full_path,PDO::PARAM_STR);
if($stmt->execute()){
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
数据库将如下所示
$sql ="CREATE TABLE IF NOT EXISTS $imagegallery (
imggall_id int(40) NOT NULL AUTO_INCREMENT PRIMARY KEY,
imggall_imageurl VARCHAR(1000) NOT NULL,
imggall_thumnailurl VARCHAR(1000) NOT NULL);" ;
$dbh->exec($sql);
UPDATE
我尝试过的新代码
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["bigimage"]['tmp_name'] && $_FILES["thumbnailimage"]['tmp_name'] )){
$folder = "images/imagegallery/";
$filebi = basename( $_FILES['bigimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fullbi_path = $folder.$filebi;
$fullti_path = $folder.$fileti;
if(move_uploaded_file($_FILES['bigimage']['tmp_name'], $fullbi_path && $_FILES['thumbnailimage']['tmp_name'], $fullti_path )) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl,imggall_thumnailurl) VALUES (?,?)");
$stmt->bindValue(1,$fullbi_path,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_path,PDO::PARAM_STR);
if($stmt->execute()){
header("Location: dashboard.php");
exit;
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="bigimage" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="thumbnailimage" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
快速谷歌:
http://php.net/manual/en/features.file-upload.multiple.php#53240
展示了一种清晰简单的多张图片上传方法。
报价:
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;
}
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['ufile']);
foreach ($file_ary as $file) {
print 'File Name: ' . $file['name'];
print 'File Type: ' . $file['type'];
print 'File Size: ' . $file['size'];
}
}
给你的:
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
然后使用前面链接的函数
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['ufile']);
$main = $file_ary[0]['name'];
$thumb = $file_ary[1]['name'];
// Upload to DB
}
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image </label>
</td>
<td valign="top">
<input type="file" name="bigimage" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image </label>
</td>
<td valign="top">
<input type="file" name="thumbnailimage" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["bigimage"]['tmp_name'] )){
$folder = "images/imagegallery/";
$filebi = basename( $_FILES['bigimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fullbi_path = $folder.$filebi;
$fullti_path = $folder.$fileti;
if(move_uploaded_file($_FILES['bigimage']['tmp_name'], $fullbi_path)) {
if(move_uploaded_file($_FILES['thumbnailimage']['tmp_name'], $fullti_path )) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl,imggall_thumnailurl) VALUES (?,?)");
$stmt->bindValue(1,$fullbi_path,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_path,PDO::PARAM_STR);
if($stmt->execute()){
header("Location: dashboard.php");
exit;
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
任何人都可以尝试一下,我能够得到什么
如果在向.php发送超过 1 张图像时遇到困难,那么在输入名称的末尾加上 [] 至关重要。以为我应该提到我花了几个小时想知道为什么 PHP 没有检测到超过 1 个文件。