上传多个文件pdo

2023-12-31

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

你怎么认为?脚本是否受保护?

这是我的 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>';

            }

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

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

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

像这样进行多重上传。

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++;
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

上传多个文件pdo 的相关文章