如何在php中的mysql数据库中上传/存储多个图像 [英] How to upload/store multiple images in mysql database in php

查看:76
本文介绍了如何在php中的mysql数据库中上传/存储多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我正在制作一个表单,用户可以上传多张图片

以下HTML代码



< input type ='file'name ='attachPhoto1'multiple />



p>

  $ tmp_file = $ _FILES ['attachPhoto1'] ['tmp_name']; 
$ target_file = basename($ _ FILES ['attachPhoto1'] ['name']);
$ upload_dir =userUploads;
move_uploaded_file($ tmp_file,$ upload_dir。/。$ target_file);

但是当我上传多个图片时,只有一张图片上传到 userUploads 文件夹。所以请帮我解决这个问题。 解决方案

请参阅此代码以在指定文件夹中上传多个图像

 <?php 
if(isset($ _ POST ['submit'])){
$ j = 0 ; //用于索引上传图片的变量。
$ target_path =uploads /; //为上传的图像声明路径。
for($ i = 0; $ i< count($ _ FILES ['file'] ['name']); $ i ++){
//循环获取数组中的单个元素
$ validextensions = array(jpeg,jpg,png); //允许的扩展。
$ ext = explode('。',basename($ _ FILES ['file'] ['name'] [$ i])); //从点(。)爆炸文件名
$ file_extension = end($ ext); //将扩展存储在变量中。
$ target_path = $ target_path。 md5(uniqid())。 。 $ ext [count($ ext) - 1]; //用一个新的图像名称设置目标路径。
$ j = $ j + 1; //根据数组中的文件增加上传图像的数量。
if(($ _FILES [file] [size] [$ i]< 100000)//可以上传大约100kb的文件
&& in_array($ file_extension, )$ {
if(move_uploaded_file($ _ FILES ['file'] ['tmp_name'] [$ i],$ target_path)){
//如果文件移动到上传文件夹。
echo $ j。 ')。< span id =noerror>图片上传成功!。< / span>< br />< br />';
} else {//如果文件未被移动。
echo $ j。 ')。< span id =error>请再试一次!。< / span>< br />< br />';
}
} else {//如果文件大小和文件类型不正确。
echo $ j。 ')。< span id =error> ***无效的文件大小或类型***< / span>< br />< br />';
}
}
}
?>


Here I'm making a form in which user can upload multiple images

HTML Code below

<input type='file' name='attachPhoto1' multiple />

PHP Code below

        $tmp_file = $_FILES['attachPhoto1']['tmp_name'];
        $target_file = basename($_FILES['attachPhoto1']['name']);
        $upload_dir = "userUploads";
        move_uploaded_file($tmp_file,$upload_dir."/".$target_file);

But when I upload more than one image, then only one image get uploaded into userUploads folder. So please help me to solve this problem.

解决方案

Refer this code for uploading more than one images in specified folder

<?php
if (isset($_POST['submit'])) {
$j = 0;     // Variable for indexing uploaded image.
$target_path = "uploads/";     // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i]));   // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
$j = $j + 1;      // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 100000)     // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
// If file moved to uploads folder.
echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else {     //  If File Was Not Moved.
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else {     //   If File Size And File Type Was Incorrect.
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}
?>

这篇关于如何在php中的mysql数据库中上传/存储多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆