gpt4 book ai didi

php - 上传多张带有描述的图片?

转载 作者:行者123 更新时间:2023-11-29 10:43:46 26 4
gpt4 key购买 nike

是否可以在 php 中上传多张图片及其描述?

enter image description here

这是我的代码:

if(isset($_FILES['files'])){
$gn_id=$_POST['gn_id'];
$date=date('Y-m-d');
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT INTO `add_gallery` (`id`, `gn_id`, `photo`, `desc`, `date`) VALUES ('', '$gn_id', '$file_name', '$desc', '$date')";

$desired_dir="gallery";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("../$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("../$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"../$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="../$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$qu=mysql_query($query);
}else{
print_r($errors);
}
}
}

最佳答案

您可以通过将描述值作为数组发布来完成此任务。

<input name="description[]" value="First Image" />
<input name="description[]" value="Second Image" />
<input name="description[]" value="Third Image" />
<input name="description[]" value="Fourth Image" />
<input name="description[]" value="Fifth Image" />

然后

$_POST['description'][0] == 'First Image'
$_POST['description'][3] == 'Fourth Image'

一旦你发布了这些值,你就可以进入你的 foreach 循环。在循环中,您可以使用当前索引描述数组获取当前图像描述

// Getting description values as arrays
$Description_Values = $_POST['description'];
// Below iteration counter
$Counter = 0;
// This is your foreach loop which you are using for images
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
// Getting Current Image Description
$current_image_description = $Description_Values[$Counter];
$Counter++;
}

关于php - 上传多张带有描述的图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45004429/

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