【问题标题】:image uploading issue in codeigniter 2.1.0codeigniter 2.1.0 中的图片上传问题
【发布时间】:2012-02-07 08:59:06
【问题描述】:

我正在为我的一个项目使用 CI 2.1.0 和 mysql 数据库。我的图片上传方法有问题。我上传的图像应该保存在上传目录中,并创建图像的缩略图版本,图像路径应该保存在数据库中。 我完成的代码工作正常,但有一个问题是,当我上传图像时,在上传目录中我得到同一张图像的两个副本,而在拇指目录中,我得到上传图像的一个副本。我只想拥有图像的一份副本,而不是那两份。 这是我的代码->

型号:

function do_upload() //to upload images in upload directory
        {
            $i=$this->db->get('portfolio')->num_rows();
            $i=$i+1;
            $image_path=realpath(APPPATH . '../uploads');
            $config=array(
                'allowed_types'=>'jpeg|png|gif|jpg',
                'upload_path'=>$image_path,
                'max_size'=>2097152,
                'file_name'=>'_'.$i.'_'
            );
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $image_data = $this->upload->data();
            $config=array(
                'source_image'=>$image_data['full_path'],
                'new_image'=>$image_path.'/thumbs',
                'maintain_ration'=>TRUE,
                'width'=>150,
                'height'=>100
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            if( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                return $error;
            }
            else
            {
                return $image_data;
            }
        }


请告诉我为什么要上传两个图像副本。 还有一件事,如果存在同名的图像,我希望图像被覆盖。我已将system->libraries 中的upload.php 文件更改为此

public $overwrite               = TRUE;

但它不起作用。有人请帮忙。

【问题讨论】:

    标签: php codeigniter-2 image-uploading


    【解决方案1】:

    你正在调用 $this->upload->do_upload() 两次..

    请试试这个代码

    警告:未经测试

    function do_upload() 
    
            {
                $i=$this->db->get('portfolio')->num_rows();
                $i=$i+1;
    
                $image_path=realpath(APPPATH . '../uploads');
    
                $config=array(
                    'allowed_types'=>'jpeg|png|gif|jpg',
                    'upload_path'=>$image_path,
                    'max_size'=>2097152,
                    'overwrite'=>TRUE,
                    'file_name'=>'_'.$i.'_'
                );
    
    
                $this->load->library('upload', $config);
    
    
            if( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());
                    return $error;
                }
                else
                {
    
            $image_data = $this->upload->data();
                $config=array(
                    'source_image'=>$image_data['full_path'],
                    'new_image'=>$image_path.'/thumbs',
                    'maintain_ration'=>TRUE,
                    'width'=>150,
                    'height'=>100
                );
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                    return $image_data;
                }
            }
    

    【讨论】:

      【解决方案2】:

      我将提供一个备用上传器类来正确处理文件上传。您可以在任何地方重复使用此代码。

      <?php
      
      //Save file as Uploader.php
      //File Uploading Class
      
      class Uploader
      {
      private $destinationPath;
      private $errorMessage;
      private $extensions;
      private $allowAll;
      private $maxSize;
      private $uploadName;
      private $seqnence;
      public $name='Uploader';
      public $useTable =false;
      
      function setDir($path){
      $this->destinationPath = $path;
      $this->allowAll = false;
      }
      
      function allowAllFormats(){
      $this->allowAll = true;
      }
      
      function setMaxSize($sizeMB){
      $this->maxSize = $sizeMB * (1024*1024);
      }
      
      function setExtensions($options){
      $this->extensions = $options;
      }
      
      function setSameFileName(){
      $this->sameFileName = true;
      $this->sameName = true;
      }
      function getExtension($string){
      $ext = "";
      try{
      $parts = explode(".",$string);
      $ext = strtolower($parts[count($parts)-1]);
      }catch(Exception $c){
      $ext = "";
      }
      return $ext;
      }
      
      function setMessage($message){
      $this->errorMessage = $message;
      }
      
      function getMessage(){
      return $this->errorMessage;
      }
      
      function getUploadName(){
      return $this->uploadName;
      }
      function setSequence($seq){
      $this->imageSeq = $seq;
      }
      
      function getRandom(){
      return strtotime(date('Y-m-d H:iConfused')).rand(1111,9999).rand(11,99).rand(111,999);
      }
      function sameName($true){
      $this->sameName = $true;
      }
      function uploadFile($fileBrowse){
      $result = false;
      $size = $_FILES[$fileBrowse]["size"];
      $name = $_FILES[$fileBrowse]["name"];
      $ext = $this->getExtension($name);
      if(!is_dir($this->destinationPath)){
      $this->setMessage("Destination folder is not a directory ");
      }else if(!is_writable($this->destinationPath)){
      $this->setMessage("Destination is not writable !");
      }else if(empty($name)){
      $this->setMessage("File not selected ");
      }else if($size>$this->maxSize){
      $this->setMessage("Too large file !");
      }else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
      
      if($this->sameName==false){
      $this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
      }else{
      $this->uploadName= $name;
      }
      if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){
      $result = true;
      }else{
      $this->setMessage("Upload failed , try later !");
      }
      }else{
      $this->setMessage("Invalid file format !");
      }
      return $result;
      }
      
      function deleteUploaded(){
      unlink($this->destinationPath.$this->uploadName);
      }
      
      }
      
      ?>
      

      使用 Uploader.php

      <?php
      
      $uploader = new Uploader();
      $uploader->setDir('uploads/images/');
      $uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list//
      $uploader->setMaxSize(.5); //set max file size to be allowed in MB//
      
      if($uploader->uploadFile('txtFile')){ //txtFile is the filebrowse element name //
      $image = $uploader->getUploadName(); //get uploaded file name, renames on upload//
      
      }else{//upload failed
      $uploader->getMessage(); //get upload error message
      }
      
      
      ?> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 2012-07-14
        • 2013-08-23
        • 2011-06-08
        • 2012-01-22
        相关资源
        最近更新 更多