同时上传两个图像,一行php pdo
数据库将如下所示同时上传两个图像,一行php pdo,php,jquery,database,image,pdo,Php,Jquery,Database,Image,Pdo,数据库将如下所示 function AddImageGallery(){ global $dbh; if(is_uploaded_file($_FILES["image"]['tmp_name'])){ $folder = "images/imagegallery/"; $file = basename( $_FILES['image']['name']); $full_path = $folder.$file; i
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);
函数AddImageGallery(){
全球$胸径;
如果(是上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
$folder=“images/imagegallery/”;
$filebi=basename($_文件['bigimage']['name']);
$fileti=basename($_文件['thumbnailimage']['name']);
$fileti=basename($_文件['thumbnailimage']['name']);
$fullbi_path=$folder.$filebi;
$fullti_path=$folder.$fileti;
如果(移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\{
echo“上传成功,我们有图像!”;
$stmt=$dbh->prepare(“插入到imagegallery(imggall\u imageurl,imggall\u thumnailurl)值(?,)”;
$stmt->bindValue(1,$fullbi_路径,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_路径,PDO::PARAM_STR);
如果($stmt->execute()){
标题(“位置:dashboard.php”);
出口
回波“图像记录”;
}否则{
echo“未记录图像”;
}
}否则{
echo“已收到上载!但处理失败”;
}
}否则{
echo“上传失败!未上传任何内容”;
}
}
图像库
大形象
缩略图
快速谷歌:
显示了一种清晰简单的多图像上载方法
引述:
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>
图像库
大形象
缩略图像
函数AddImageGallery(){
全球$胸径;
如果(是上传的文件($文件[“bigimage”]['tmp\U名称]){
$folder=“images/imagegallery/”;
$filebi=basename($_文件['bigimage']['name']);
$fileti=basename($_文件['thumbnailimage']['name']);
$fileti=basename($_文件['thumbnailimage']['name']);
$fullbi_path=$folder.$filebi;
$fullti_path=$folder.$fileti;
如果(移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\{
如果(移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\{
echo“上传成功,我们有图像!”;
$stmt=$dbh->prepare(“插入到imagegallery(imggall\u imageurl,imggall\u thumnailurl)值(?,)”;
$stmt->bindValue(1,$fullbi_路径,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_路径,PDO::PARAM_STR);
如果($stmt->execute()){
标题(“位置:dashboard.php”);
出口
回波“图像记录”;
}否则{
echo“未记录图像”;
}
}
}否则{
echo“已收到上载!但处理失败”;
}
}否则{
echo“上传失败!未上传任何内容”;
}
}
任何人都可以尝试一下,如果一个人在向.php发送多个图像时遇到困难,我可以得到什么。在输入名称的末尾添加[]是至关重要的。 我想我应该提到的是,在过去的几个小时里,我一直在想为什么PHP检测不到超过1个文件
$\u文件['user']
非$\u文件['image']['name']
要上载2个,请使用foreach($\u文件['user']作为$img)
更新问题对不起,错误的信息我尝试了不同的方法我忘记将其更改为原始代码…现在我更新了问题,请更具体地回答问题否您不能对两个字段使用相同的输入名称,改为使用
我将如何将其保存到数据库中的一行?如果我这样使用它,它将保存在两行中,对吗?请阅读问题您的链接上载方式与问题不同,它将上载多个问题,但具有不同的id不同的行我想要的是一个id和一行,请仔细阅读..这取决于您和您如何使用构建数据库。列:id、img1、img2。只需上传$file\u-ary[0]和$file\u-ary[1]我很难理解你到底想要什么。我有两个上传按钮,每个都有相同的图像,但大小不同,一个用于大图,一个用于缩略图,现在我想在一行中同时保存两个图像,这样我就可以在滑块中一起使用它们了。所以这是完全一样的图像,你有两个不同的按钮,每个图像一个?为什么不在php中调整图像的大小,或者在css中为图像指定宽度和高度?
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>
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";
}
}