【问题标题】:Uploading an image in codeigniter在 codeigniter 中上传图片
【发布时间】:2010-10-31 13:11:06
【问题描述】:

我正在尝试上传图片,创建缩略图,但出现错误。

这是我的控制器。

<?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:/wamp/www/uploads/Heaven_Clouds.jpg,/pictures/kFttl7lpE7Rk.jpg)[function.rename]:没有这样的文件或目录

文件名:models/Process_image.php

行号:68

这是第 68 行:

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

【问题讨论】:

  • 我一直在摆弄 CI 缩略图一段时间,并且不想在某些教程中陷入困境 - 谢谢!

标签: image codeigniter


【解决方案1】:

去掉“picutres”前面的“/”

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

它想说你想把你重命名的文件放在一个名为“pictures”的目录中,该目录位于 Unix 文件系统的根目录下,那么你显然在 Windows 系统中,你似乎没有“pictures”磁盘根目录中的目录。

结果:

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

【讨论】:

  • 很高兴阅读。不要忘记将您的问题标记为已回答^^
【解决方案2】:

这是一个非常简单的脚本,部分来自 CI Docs:

          $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 循环。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,但在我的场景中,我只需要上传文件数组,所以我对库“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;
    

    如果您要上传单个文件,则复制 system/libraries/Upload.php 中的函数

    函数 do_upload($field) 到函数 do_upload_array($field, $i)

    将 [$i] 索引放在第 160、162、196、197 行

    函数 _file_mime_type($_FILES[$field]) 到函数 _file_mime_type_array($_FILES[$field], $i)

    并将 [$i] 索引放在第 1026、1043、1057 和 1065 行

    就是这样……

    您的文件数组现在可以轻松上传了....

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 2013-08-23
      • 2011-06-08
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多