2010-10-31 113 views
2

我想上传一个图像,创建缩略图,但我得到一个错误。上传codeigniter中的图像

这是我的控制器。

<?php 
class Upload extends Controller { 

    function Upload() 
    { 
     parent::Controller(); 
     $this->load->helper(array('form','url','file')); 


    } 

    function index() 
    { 

     $this->load->view('upload_form'); //Upload Form 

    } 

    function picupload() 
    { 
     //Load Model 
     $this->load->model('Process_image'); 

     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '2048'; //2 meg 


     $this->load->library('upload'); 

     foreach($_FILES as $key => $value) 
     { 
      if(! empty($key['name'])) 
      { 
       $this->upload->initialize($config); 

       if (! $this->upload->do_upload($key)) 
       { 
        $errors[] = $this->upload->display_errors(); 

       }  
       else 
       { 

        $this->Process_image->process_pic(); 

       } 
      } 

     } 


     $data['success'] = 'Thank You, Files Upladed!'; 

     $this->load->view('upload_success', $data); //Picture Upload View 




    } 
    } 
    ?> 

我的模型:

<?php 
class Process_image extends Model { 

    function Process_image() 
    { 
     parent::Model(); 

     $this->load->library('image_lib'); 
     //Generate random Activation code 

     function generate_code($length = 10){ 

       if ($length <= 0) 
       { 
        return false; 
       } 

       $code = ""; 
       $chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; 
       srand((double)microtime() * 1000000); 
       for ($i = 0; $i < $length; $i++) 
       { 
        $code = $code . substr($chars, rand() % strlen($chars), 1); 
       } 
       return $code; 

       } 

    } 

function process_pic() 
    { 
     //Connect to database 
     $this->load->database(); 

     //Get File Data Info 
     $uploads = array($this->upload->data()); 

     $this->load->library('image_lib'); 

     //Move Files To User Folder 
     foreach($uploads as $key[] => $value) 
     { 

         //Gen Random code for new file name 
      $randomcode = generate_code(12); 

      $newimagename = $randomcode.$value['file_ext']; 

      //Creat Thumbnail 
      $config['image_library'] = 'GD2'; 
      $config['source_image'] = $value['full_path']; 
      $config['create_thumb'] = TRUE; 
      $config['thumb_marker'] = '_tn'; 
      $config['master_dim'] = 'width'; 
      $config['quality'] = 75; 
      $config['maintain_ratio'] = TRUE; 
      $config['width'] = 175; 
      $config['height'] = 175; 
      $config['new_image'] = '/pictures/'.$newimagename; 

      //$this->image_lib->clear(); 
      $this->image_lib->initialize($config); 
      //$this->load->library('image_lib', $config); 
      $this->image_lib->resize(); 

      //Move Uploaded Files with NEW Random name 
      rename($value['full_path'],'/pictures/'.$newimagename); 

      //Make Some Variables for Database 
      $imagename = $newimagename; 
      $thumbnail = $randomcode.'_tn'.$value['file_ext']; 
      $filesize = $value['file_size']; 
      $width = $value['image_width']; 
      $height = $value['image_height']; 
      $timestamp = time(); 

      //Add Pic Info To Database 
      $this->db->set('imagename', $imagename); 
      $this->db->set('thumbnail', $thumbnail); 
      $this->db->set('filesize', $filesize); 
      $this->db->set('width', $width); 
      $this->db->set('height', $height); 
      $this->db->set('timestamp', $timestamp); 

      //Insert Info Into Database 
      $this->db->insert('pictures'); 

     } 



    } 
    } 
    ?> 

错误:遇到

甲PHP错误

严重性:警告

消息:重命名(C:/瓦帕/万维网/uploads/Heaven_Clouds.jpg,/pictures/kFttl7lpE7Rk.jpg)[function.rename]:没有这样的文件或目录

文件名:型号/ Process_image.php

行号:68

这是第68行:

rename($value['full_path'],'/pictures/'.$newimagename); 
+1

我一直在用了一段时间CI缩略图摆弄现在,并没有想肘深的一些教程 - 感谢这! – t0mgs 2013-12-28 11:47:09

回答

5

rename($value['full_path'],'/pictures/'.$newimagename); 
删除 “/” 前 “picutres”

它想要说你想把你的重命名文件放在一个名为“图片”的目录放在Unix文件的根目录下ystem,那么你显然是在Windows系统中,并且在磁盘的根目录下似乎没有“图片”目录。

结果:

rename($value['full_path'],'pictures/'.$newimagename); 
+0

感谢它的工作。 – Gandalf 2010-10-31 13:33:08

+0

很高兴看到它。不要忘记将你的问题标记为回答^^ – 2010-10-31 13:35:07

0

这是从CI文档部分采取了非常简单的脚本:

  $config['upload_path'] = 'uploads/cgm/'; 
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
      $config['max_size'] = '0'; 
      $config['max_width'] = '0'; 
      $config['max_height'] = '0'; 
      $this->load->library('upload', $config); 


      $configThumb = array(); 
      $configThumb['image_library'] = 'gd2'; 
      $configThumb['source_image'] = ''; 
      $configThumb['create_thumb'] = TRUE; 
      $configThumb['maintain_ratio'] = TRUE; 

      $configThumb['width'] = 100; 
      $configThumb['height'] = 120; 
      $this->load->library('image_lib'); 

      for($i = 1; $i < 6; $i++) { 
      $upload = $this->upload->do_upload('file'.$i); 
      if($upload === FALSE) continue; 
      $data = $this->upload->data(); 

      $uploadedFiles[$i] = $data; 

      $imgName = $this->pictures_m->addPicture(array('listing_id' => $listing_id, 'ext' => $data['file_ext'], 'picture_name' => $this->input->post('file'.$i.'name'))); 

      if($data['is_image'] == 1) { 
       $configThumb['source_image'] = $data['full_path']; 
       $configThumb['new_image'] = $data['file_path'].$imgName.$data['file_ext']; 
       $this->image_lib->initialize($configThumb); 
       $this->image_lib->resize(); 
      } 
    rename($data['full_path'], $data['file_path'].$imgName.$data['file_ext']); 

      } 

这将需要5张图片,但如果你只有一个,你可以改变for循环。

0

我有同样的问题,但是,在我的情况,我只能上载文件的阵列,所以我做了一些小的变化到库“upload.php的”

视图

<form encrypt="multipart/form-data" ...> 
<input type="file" name="your_name[]" /> 
<input type="file" name="your_name[]" /> 
<input type="submit" /> 
</form> 

控制器

for($i = 0; $i < count($_FILES['your_name']['name']); $i++) { 
    $config['upload_path'] = 'your upload path'; 
    $this->upload->initialize($config); 
    $this->upload->do_upload('listing_images', $i); 
endfor; 

如果您有单个文件上传,然后复制系统/图书馆/上传功能。PHP

功能do_upload($字段)来起作用do_upload_array($字段,$ⅰ)

看跌[$ i]于索引上线160,162,196,197

功能_file_mime_type($ _ FILES [$场])来起作用_file_mime_type_array($ _ FILES [$字段],$ⅰ)

,把[$ i]于上线1026,1043,1057和1065

索引完蛋了......

文件阵列容易现在上传....