gpt4 book ai didi

php - 使用一个输入字段上传多张图片

转载 作者:可可西里 更新时间:2023-11-01 13:13:43 26 4
gpt4 key购买 nike

我创建了一个带有管理页面的摄影网站,该页面将照片上传到 mysql 表中的不同类别。这很有用,但我一次只能上传一个文件,我希望能够选择多张图片。

这是表格

<form action="index.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">

<select name="category" id="category">
<option value="Nature">Nature</option>
<option value="People">People</option>
<option value="Abstract">Abstract</option>
</select>

<input type="file" name="fileField" id="fileField" />

<input type="submit" name="submit" id="submit" value="Add Images" />

</form>

这是解析表单的 php

if (isset($_POST['submit'])) {

$category = mysql_real_escape_string($_POST['category']);
// Add this product into the database now
$sql = mysql_query("INSERT INTO images (category, date_added)
VALUES('$category',now())") or die (mysql_error());


$pid = mysql_insert_id();
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../photos/$newname");
header("location: thumbnail_generator.php");
exit();
}

我查看了 html5 文件输入法,据我所知,我可以按以下方式更改输入:

<input type="file" name="fileField[]" id="fileField" multiple="multiple"/>

这允许我在网站上选择多个文件,但我不知道如何在我的 php.ini 中实现它。任何帮助将不胜感激。

最佳答案

<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
Add Photos <input multiple="true" onchange="this.form.submit()" type="file" name="photos[]"/>

<input type="hidden" name="sendfiles" value="Send Files" />
</form>

<?php
define ("MAX_SIZE","5000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;

if(isset($_POST['sendfiles']))
{
$uploaddir = "files/"; //a directory inside
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
echo "\n This is the extension: ",$extension;
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
?>
<h4>Unknown extension!</h4>
<?php
$errors=1;
}
else
{
$size=filesize($_FILES['photos']['tmp_name'][$name]);
if ($size > MAX_SIZE*1024)
{
?>
<h4>You have exceeded the size limit!</h4>
<?php
$errors=1;
}
$image_name=$filename.'.'.$extension;
$newname="files/".$image_name;
$copied = copy($_FILES['photos']['tmp_name'][$name], $newname);
if (!$copied)
{
?>
<h4>Copy unsuccessfull!</h4>
<?php
$errors=1;
}

}

}
//echo "Images uploaded successfully";
}
//mysql_close($dbhandle);
?>

关于php - 使用一个输入字段上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9531856/

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