gpt4 book ai didi

javascript - PHP 将多个带有标题的文件上传到 MySQL

转载 作者:行者123 更新时间:2023-11-29 07:33:37 26 4
gpt4 key购买 nike

我编写了一个脚本来将图像存储在我的数据库中。这些图像有一个标题,该标题也被上传和存储。这很容易开始工作。我有一个 jquery 函数设置,每次单击按钮时都会添加新的文件输入和标题输入。这也有效。

不起作用的是我的 PHP 由于某种原因没有上传多个文件。

有人可以告诉我我做错了什么吗?

谢谢 :)

HTML:

<form id="uploadMultiple" method="post" action="/scripts/php/imageUploadTest" enctype="multipart/form-data">
<table class="fileUploadTable" id="uploadArea" cellspacing="0">
<tr>
<td>
<label for="imageFile">Image:</label>
<input type="file" name="imageFile" accept=".jpg,.jpeg,.png,.gif">
</td>
<td>
<label for="imageCaption">Image Caption:</label>
<input type="text" name="imageCaption">
</td>
<td width="150px">
&nbsp;
</td>
</tr>
<tr id="uploadSubmission">
<td>
<input type="submit" value="Upload More" id="uploadMore">
</td>
<td>
<input type="submit" value="Submit" name="addImage">
</td>
<td width="150px">
&nbsp;
</td>
</tr>
</table>
</form>

用于添加新元素的 JQuery:

$(function() {
var scntDiv = $('#uploadArea');
var i = $('#p_scents tr td').size() + 1;

$('#uploadMore').live('click', function() {
$('<tr><td><label for="imageFile">Image:</label> <input type="file" name="imageFile" accept=".jpg,.jpeg,.png,.gif"></td><td><label for="imageCaption">Image Caption:</label> <input type="text" name="imageCaption"></td><td><a href="#" class="removeUpload" width="150px" style="text-align: center">Remove</a></td></tr>').insertBefore( $('#uploadSubmission') );
i++;
return false;
});

$('.removeUpload').live('click', function() {
if( i > 1 ) {
$(this).parents('tr').remove();
i--;
}
return false;
});
});

最后是 PHP:

require($_SERVER['DOCUMENT_ROOT'].'/settings/globalVariables.php');
require($_SERVER['DOCUMENT_ROOT'].'/settings/mysqli_connect.php');


$db_name = 'imageUploads';
$tbl_name = 'gallery';
if(!$conn)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn, "$db_name")or die("cannot select DB");

foreach($_FILES['imageFile'] as $file){
$caption = $_POST['imageCaption'];
$uploadDir = 'http://www.example.com/images/'.'gallery/';
$fileName = $_FILES['imageFile']['name'];
$filePath = $uploadDir . $fileName;

if(move_uploaded_file($_FILES["imageFile"]["tmp_name"],$_SERVER['DOCUMENT_ROOT']."/images/gallery/".$_FILES["imageFile"]["name"]))
{
$query_image = "INSERT INTO $tbl_name(filename,path,caption) VALUES ('$fileName','$uploadDir','$caption')";
if(mysqli_query($conn, $query_image))
{
echo "Stored in: " . "gallery/" . $_FILES["imageFile"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}

我希望它能通过我的 foreach 循环正常处理多个图像,但即使我选择了 4 个图像,它也只上传一张图像。

编辑:

我尝试将我的代码修改为此,但它也不起作用,但查看教程,这似乎比我以前的代码更正确:

require($_SERVER['DOCUMENT_ROOT'].'/settings/globalVariables.php');
require($_SERVER['DOCUMENT_ROOT'].'/settings/mysqli_connect.php');


$db_name = 'imageUploads';
$tbl_name = 'gallery';
if(!$conn)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn, "$db_name")or die("cannot select DB");

foreach($_FILES['imageFile'] as $key => $name){
$caption = $_POST['imageCaption'];
$uploadDir = 'http://www.jollyrogerpcs.com/images/'.'gallery/';
$fileName = $key.$_FILES['imageFile']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type= $_FILES['files']['type'][$key];
$filePath = $uploadDir . $fileName;

if(move_uploaded_file($file_tmp,$_SERVER['DOCUMENT_ROOT']."/images/gallery/".$fileName))
{
$query_image = "INSERT INTO $tbl_name(filename,path,caption) VALUES ('$fileName','$uploadDir','$caption')";
if(mysqli_query($conn, $query_image))
{
echo "Stored in: " . "gallery/" . $_FILES["imageFile"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}

我还在需要它们的 HTML 输入元素的名称中添加了 []

在 frz3993 的帮助下,这里是完整的工作代码:

require($_SERVER['DOCUMENT_ROOT'].'/settings/globalVariables.php');
require($_SERVER['DOCUMENT_ROOT'].'/settings/mysqli_connect.php');


$db_name = 'imageUploads';
$tbl_name = 'gallery';
if(!$conn)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn, "$db_name")or die("cannot select DB");

$uploadDir = 'http://www.example.com/images/gallery/';

foreach ($_FILES['imageFile']['name'] as $key => $value) {

if($_FILES['imageFile']['error'][$key] == UPLOAD_ERR_OK){

$caption = $_POST['imageCaption'][$key];
$fileName = $_FILES['imageFile']['name'][$key];
$file_size = $_FILES['imageFile']['size'][$key];
$file_tmp = $_FILES['imageFile']['tmp_name'][$key];
$file_type= $_FILES['imageFile']['type'][$key];
$filePath = $uploadDir.$fileName;

if(move_uploaded_file($file_tmp,$_SERVER['DOCUMENT_ROOT']."/images/gallery/".$fileName))
{
$query_image = "INSERT INTO $tbl_name(filename,path,caption) VALUES ('$fileName','$uploadDir','$caption')";
mysqli_query($conn, $query_image);

}
}
}

最佳答案

就像我在评论中所写的$_FILES['imageFile']将包含有关上传文件的一些信息。例如,我将使用四个元素(因为我不记得其中还有什么)

array('name' => fileName, 'tmp_name' => temporaryName, 'error' => uploadError, 'size' => theFileSize);

当您在 $_FILES 上使用 foreach 时,您实际上是在循环这四个元素,因此循环中的行将运行四次。

基本上,您无法上传或发布具有相同名称的输入,因为它会被覆盖。但您可以使用数组作为输入名称。对于您的情况 name=imageFile[]name=imageCaption[]。假设您有两个上传的 image1.jpgimage2.jpg,其标题为 caption1caption2

$_FILES['imageFile']$_POST['imageCaption'] 结构如下:

$_FILES['imageFile'] = array('name' => array(
0 => 'image1.jpg',
1 => 'image2.jpg'
));

$_POST['imageCaption'][0] = caption1;
$_POST['imageCaption'][1] = caption2;

所以你的 foreach 循环应该看起来像

$uploadDir = 'http://www.jollyrogerpcs.com/images/gallery/'; 

foreach ($_FILES['imageFile']['name'] as $key => $value) {

if($_FILES['imageFile']['error'][$key] == UPLOAD_ERR_OK){

$caption = $_POST['imageCaption'][$key];
$fileName = $_FILES['imageFile']['name'][$key];
$file_size = $_FILES['imageFile']['size'][$key];
$file_tmp = $_FILES['imageFile']['tmp_name'][$key];
$file_type= $_FILES['imageFile']['type'][$key];
$filePath = $uploadDir.$fileName;

//your move uploaded file and sql query goes here

}

}

关于javascript - PHP 将多个带有标题的文件上传到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com