上传多个文件pdo

时间:2017-05-08 04:51:52

标签: php mysql pdo

如何使用此脚本在数据库中上传多个文件 我想使输入文件和数据库不是必需的

你怎么看?脚本受到保护吗?

这是我的php pdo脚本:

        $file         = $_FILES['file'];
        $fileName     = $_FILES['file']['name'];
        $fileTmpName  = $_FILES['file']['tmp_name'];
        $fileSize     = $_FILES['file']['size'];
        $fileError    = $_FILES['file']['error'];
        $fileType     = $_FILES['file']['type'];

        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));
        $allowed = array('jpg', 'jpeg', 'png', 'pdf');
    $formErrors = array();
        if (in_array($fileActualExt, $allowed)) {
          if ($fileError === 0) {
            if ($fileSize < 1000000) {

              $fileNameNew = uniqid('', true).".".$fileActualExt;
              $fileDestination = 'images/' .$fileNameNew;
              move_uploaded_file($fileTmpName, $fileDestination);
            } else {
              $formErrors[] = "file size must be under 2mb";
            }
          } else {
            $formErrors[] = 'error';
          }
        } else {
          $formErrors[] 'file not uploaded..!';
        }

这是html:

  <tr>
  <td><label class="control-label">Profile Img.</label></td>
    <td><input class="input-group" type="file" name="file" multiple accept="image/*" /></td>
</tr> 

插入数据库

             if (empty($formErrors)) {
              $stmz = $con->prepare("INSERT INTO reports(title, username, email, mobile, repdate,photo, descr) 
                                                  VALUE(:ztitle, :zusername, :zemail, :zmobile, NOW(), :uphoto, :zdescr)");
              $stmz->execute(array(

                'ztitle'    => $title,
                'zusername' => $username,
                'zemail'    => $email,
                'zmobile'   => $mobile,
                'uphoto'    => $fileNameNew,
                'zdescr'   => $descr
                ));
            /* echo "<meta http-equiv='refresh' content='0'>"; */ 
              if ($stmz) {

          echo '<script language="javascript">';
          echo 'alert("your report Added")';
          echo '</script>';
          echo '<div class="alert alert-danger">your report Added</div>';
              }

             } else {
          echo '<script language="javascript">';
          echo 'alert("your report not Added")';
          echo '</script>';
          echo '<div class="alert alert-danger">your report not Added</div>';

            }

2 个答案:

答案 0 :(得分:1)

当您上传多个文件时,您应该为多个文件创建一个数组。 NAME = “文件[]”

我建议您使用此基本类来上传多个文件。

https://github.com/AzCreativeWorld/Dropzone-with-Image-Resizer

Tyr喜欢多次上传。

foreach($_FILES['files']['name'] as $val)
        {
            $s++;
            $filesName      =   str_replace(" ","",trim($_FILES['files']['name'][$n]));
            $files          =   explode(".",$filesName);
            $File_Ext       =   substr($_FILES['files']['name'][$n], strrpos($_FILES['files']['name'][$n],'.'));

            if($File_Ext==".png" || $File_Ext==".jpeg" || $File_Ext==".gif" || $File_Ext==".jpg")
            {
                $srcPath    =   'uploads/'; // your DIR name
                $path       =   trim($srcPath.$fileName);
                move_uploaded_file($_FILES['files']['tmp_name'][$n], $path)
                {                   
                    // insertion query here success
                }else{
                    // file not move to the destination
                }
            }
            else
            {
                 //extention not valid
            }
            $n++;
        }

答案 1 :(得分:0)

此脚本https://github.com/e-ht/cautious-rotary-phone显示多个文件的完整上载和数据库日志过程。它使用PHP7,JS / AJAX和MySQL。

以下是多文件上传表单处理的JS片段:

 // Support for sending multiple files
    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      formData.append('files', file, file.name);
    }
相关问题