【问题标题】:how can uploaded multi images in this class [closed]如何在此类中上传多张图片[关闭]
【发布时间】:2015-10-24 05:12:13
【问题描述】:

我想在我的数据库中上传多张图片只是 url 图片代码工作正常但我只能上传一张图片我需要上传 3 张图片我在数据库表 e_img - e_img2 - e_img3 如何上传图片 url 3列

这是我的班级上传:

        <?php
       class Upload {

       private $allowedExts = array('doc','docx','pdf','txt','jpg','png');
       private $maxSize;
       private $file;
       private $uploadsDirecotry;
       private $fileUrl;
       private $filenames = array();

       function __construct($file,$allowedExts,$uploadsDirecotry,$maxSize){
           if(is_array($allowedExts) AND is_int($maxSize)){
             $this->file  = $file;
             $this->allowedExts = $allowedExts;
             $this->maxSize = $maxSize;
             $this->uploadsDirecotry = $uploadsDirecotry;

           }else{
             throw new Exception("File extension must be in an array and max size value must be intger value.");
           }

           }
       function uploadFiles(){

            $file = $this->file;
            $allowedExts = $this->allowedExts;
            $maxsize = $this->maxSize;
            $uploadsDir = $this->uploadsDirecotry;

        for($i = 0; $i < count($file['name']); $i++){

          $errors = array();

          // print_r($_FILES);
          $filename    = $file['name'][$i];
          $fileext    = strtolower(end(explode('.',$filename)));
          $filesize    = $file['size'][$i];
          $filetmpname = $file['tmp_name'][$i];

        if(in_array($fileext, $allowedExts) === FALSE){
           $errors[] = "Extension in sot allowed";
        }

         if($filesize > $maxsize){
            $errors[] = "File size must be less than {$maxsize} KB !";
         }

         if(empty($errors)){

            $random = rand(0,199);
            $this->fileUrl = $random . "_" . date("d-m-Y") . "_" . $filename;
            $destination = $uploadsDir. $random."_".date("d-m-Y") . "_" . $filename;
            move_uploaded_file($filetmpname, $destination);
            $this->filenames[] = $this->fileUrl;

         }else{

           foreach($errors as $error){
            throw new Exception($error);
           }
         }
              } // end for

              return TRUE;
       }

       function getFileUrl()
       {
             return $this->fileUrl;
           }

       function getFilesNames()
       {
             return $this->filenames;
           }

       }


    ?>

还有这个 html php 文件

        <?php include 'header.php';

                if(isset($_POST['add']))
                {
                    $e_title    = $_POST['e_title'];
                    $e_user     = $userRow['user_name'];    

                 try{
                    include 'models/Upload.php';
                    $file = $_FILES['e_img'];
                    $allowedExts = array('jpg','png','gif','jpeg');
                    $uploadsDirectory = "imgupload/";
                    $maxSize = 4000000;
                    $upload = new Upload($file, $allowedExts, $uploadsDirectory, $maxSize);
                    $upload->uploadFiles();
                    $e_img = $uploadsDirectory.$upload->getFileUrl();

                  }catch(Exception $exc){
                    echo'<div class="alert alert-block alert-danger">fail image uploaded</div>';
                    exit;}


                        $insert = $user->insert($e_title,$e_img,$e_user);

                        echo"<div class='alert alert-block alert-success'>Save success</div>";
                        exit;
            }

            ?>
                <form action='newads.php' method="POST" enctype="multipart/form-data">
                   <p>إضافة صور </p>
                    <input type="file" name='e_img[]' id="exampleInputFile">
                    <input  type="submit" name='add' class='btn btn-primary' value='add' />
              </form>


    <?php include 'footer.php'; ?>  

我试试这个,但是没有成功

    <form action='newads.php' method="POST" enctype="multipart/form-data">
    <p>إضافة صور </p>
    <input type="file" name='e_img[]' id="exampleInputFile">
    <input type="file" name='e_img2[]' id="exampleInputFile">
    <input type="file" name='e_img3[]' id="exampleInputFile">
    <input  type="submit" name='add' class='btn btn-primary' value='add' />
    </form>

【问题讨论】:

    标签: php html mysql pdo


    【解决方案1】:

    您不需要增加文件字段中的字段名称。当 PHP 接收到它时,这将破坏数组。改成这个:(应该都是e_img[]

    <input type="file" name='e_img[]' id="exampleInputFile">
    <input type="file" name='e_img[]' id="exampleInputFile">
    <input type="file" name='e_img[]' id="exampleInputFile">
    

    【讨论】:

    • 但 e_img 是表中 1 列的名称,我有 3 列 e_img / e_img2 / e_img3 我想上传 3 列中的 url 图片
    • 您正在使用的上传类循环遍历文件对象数组。将[] 添加到字段名称的末尾向 PHP 表明那些匹配的对象应该在一个数组中以供进一步处理。在数组括号之前增加字段名称意味着您只会上传第一个,因为上传类只能看到该数组中的一个对象。您还为每个附加了相同的 ID,这不会影响上传,但会影响应用的任何 JS/CSS,因为 HTML ID 标签应该是唯一的。
    • 我现在有这个问题我想获取全名图片stackoverflow.com/questions/33318470/…请帮帮我
    • @HassanIbra 如果它按照您在this comment... 中所说的那样修复了它,请留在您的其他问题stackoverflow.com/q/33318470 中,然后将此答案标记为正确。
    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2016-11-22
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多