PHP上传多个图像并将其插入数据库
我想用这个代码上传多张图片,但是我不能,我想应该是用for each,但是每次我尝试的时候,错误无处不在 我想在一列中插入整个图像路径,并在每个图片“,”之后插入 下面是php代码PHP上传多个图像并将其插入数据库,php,Php,我想用这个代码上传多张图片,但是我不能,我想应该是用for each,但是每次我尝试的时候,错误无处不在 我想在一列中插入整个图像路径,并在每个图片“,”之后插入 下面是php代码 if (isset($_POST['upload_file'])) { $date = date('Y-m-d h:i:s'); if ($_FILES['image']['name'] != '') { $temp = explode('.', $_FILES['image']['name']);
if (isset($_POST['upload_file'])) {
$date = date('Y-m-d h:i:s');
if ($_FILES['image']['name'] != '') {
$temp = explode('.', $_FILES['image']['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
$image_path = '../media/file/'.$image_name;
$image_db_path = '../media/file/'.$image_name;
if ($image_size < '50000000') {
if ($image_ext == 'jpg' || $image_ext == 'png' || $image_ext =='gif' || $image_ext == 'jpeg') {
if (move_uploaded_file($image_tmp, $image_path)) {
$ins_sql = "INSERT INTO files (imgs_paths, date) VALUES ('$image_db_path', '$date')";
if (mysqli_query($conn,$ins_sql)) {
header('Location: index.php');
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
} else{
$error = '';
}
}
if(isset($\u POST['upload\u file'])){
$date=日期('Y-m-d h:i:s');
如果($\u文件['image']['name']!=''){
$temp=explode('.',$_文件['image']['name']);
$image_name=round(microtime(true))。.end($temp);
$image\u tmp=$\u文件['image']['tmp\u名称'];
$image\u size=$\u文件['image']['size'];
$image\u ext=pathinfo($image\u name,pathinfo\u EXTENSION);
$image\u path='../media/file/'.$image\u name;
$image\u db\u path='../media/file/'.$image\u name;
如果($image_size<'50000000'){
如果($image|ext='jpg'|$$image|ext='png'|$$image| ext=='gif'|$$image| ext=='jpeg'){
如果(移动上传的文件($image\u tmp,$image\u path)){
$ins_sql=“插入文件(imgs_路径,日期)值(“$image_db_路径”,“$date”)”;
if(mysqli_查询($conn$ins_sql)){
标题('Location:index.php');
}否则{
$error='';
}
}否则{
$error='';
}
}否则{
$error='';
}
}否则{
$error='';
}
}否则{
$error='';
}
}
这里是html代码
<input type="file" accept="image/*" name="image[]" multiple/>
您使用
foreach
是对的。如果你需要上传多个,你需要使用它。这很简单:
foreach($_FILES['image'] as $i => $file){
///... other code
$temp = explode('.', $_FILES['image'][$i]['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image'][$i]['tmp_name'];
$image_size = $_FILES['image'][$i]['size'];
///... rest of code
}
因为它是一个图像数组,所以需要访问每个元素。注意我们是如何使用数组索引($i
)来访问循环中的当前元素的
同样,您也可以在循环中使用
$file
,因为它也是循环中的当前元素。(而不是$\u文件['image'][$i]…
)您使用foreach
是正确的。如果你需要上传多个,你需要使用它。这很简单:
foreach($_FILES['image'] as $i => $file){
///... other code
$temp = explode('.', $_FILES['image'][$i]['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image'][$i]['tmp_name'];
$image_size = $_FILES['image'][$i]['size'];
///... rest of code
}
因为它是一个图像数组,所以需要访问每个元素。注意我们是如何使用数组索引($i
)来访问循环中的当前元素的
同样,您也可以在循环中使用
$file
,因为它也是循环中的当前元素。(而不是$\u文件['image'][$i]…
)您在html上使用了多少输入?我想让用户只在输入时选择100多张图片。对于这么多的图片,我认为您需要拖放功能。试试这个:还有:100张图片每个允许50000000字节==4.7GB。祝你好运!您可能需要重新考虑这个过程您在html上使用了多少输入?我想让用户只在输入时选择100多张图片。对于这么多的图片,我认为您需要拖放功能。试试这个:还有:100张图片每个允许50000000字节==4.7GB。祝你好运!你可能需要重新考虑这个过程注意$\u FILES['image'][$i]===$file
@riggsfully我也会在答案中去掉这个!除非在foreach中使用$file
它,否则将作为$i=>$file
执行没有多大意义。否则,它也可能是一个loop@RiggsFolly这是‘早到这里,我需要一杯咖啡’这是‘晚到这里,我需要我的便条<代码>$\u文件['image'][$I]====$file@riggsfully我也会在答案中扔掉它!除非在foreach中使用$file
它,否则将作为$i=>$file
执行没有多大意义。否则,它也可能是一个loop@RiggsFolly来早了,我要喝杯咖啡来晚了,我要我的床