php - 如何用数组上传多张图片?

标签 php html mysql

我有一个表单,如何将其一一插入数据库。例如:-

 <form method="post">
  <input type="file" name="img[]">
  <input type="file" name="img[]">
  <input type="file" name="img[]">
 </form>

最佳答案

if(isset($_POST['submit'])){
if(count($_FILES['img']['name']) > 0){
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) {
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != ""){

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];

            //save the url and the file
            $filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];

            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) {

                $files[] = $shortname;
                //insert into db 
                //use $shortname for the filename
                //use $filePath for the relative url to the file

            }
          }
    }
}

这可能对你有帮助

关于php - 如何用数组上传多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52744982/

相关文章:

php - 如何使用 PSR-4 自动加载 Composer 制作扩展类

PHP 枚举层次结构

html - parent 和 child 高度相同,为什么会有垂直滚动条?

php - 检查用户角色和用户限制

javascript - 使用 Plupload 和 Symfony3 抛出 ValidationException 时无响应

java - 从 Java 中的 xml 文件创建 html 表头

javascript - IFrame 中的 AngularJS run() 方法不会在 iframe 重新加载时重新运行

php - 在数据库中插入 header 值时出现 MYSQL 错误

mysql - SQL计算查询示例中的时间/日期

mysql - 多边形数据未插入 MySQL