【问题标题】:Codeigniter Permission denied images uploadCodeigniter 权限拒绝图片上传
【发布时间】:2016-01-28 16:59:11
【问题描述】:

我正在学习如何使用 codeigniter 上传图像,当我单击没有任何图像的上传时出现错误权限被拒绝,如果我选择图像,它会很好地工作。我只是希望它会显示我的信息是:请上传图片,你能看看我的代码并帮助我找出我的问题。 错误说: 这是我的视图代码:

<head>
<title>CI upload images</title>
<meta charset="UTF-8">
</head>
<body>
<div id="gallery">
    <?php if(isset($images) && count($images)): 
    foreach ($images as $image): ?>
    <div class="thumb">
        <a href="<?php echo $image['url']; ?>">
            <img src="<?php echo $image['thumb_url']; ?>"/>
        </a>
    </div>

    ?>
    <?php endforeach; else: ?>
        <div id="blank_gallery">Please upload an Images</div>
    <?php endif; ?>
</div>
<div id="upload">
    <?php 
    echo form_open_multipart('gallery');
    echo form_upload('userfile');
    echo form_submit('upload', 'Upload');
    echo form_close();
    ?>
</div>
</body>

这是我的控制器代码:

class Gallery extends CI_Controller{
function index(){

    $this->load->model('Gallery_model');
    if($this->input->post('upload')){
        $this->Gallery_model->do_upload();
    }

    $data['images'] = $this->Gallery_model->get_images();
    $this->load->view('gallery_view', $data);

}
}

这是我的模型

class Gallery_model extends CI_Model{

var $gallery_path;
var $gallery_path_url;

function __construct(){
    parent::__construct();

    $this->gallery_path = realpath(APPPATH . '../images');
    $this->gallery_path_url = base_url().'images/';
}
function do_upload(){
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
        );
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width' => 150,
        'height' => 100
        );
    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
}
function get_images(){
    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.','..','thumbs'));

    $images = array();

    foreach ($files as $files) {
        $images []= array(
            'url'=> $this->gallery_path_url . $files,
            'thumb_url' => $this->gallery_path_url . 'thumbs/' . $files
            );
    }
    return $images;
}
}

你能帮忙吗?

【问题讨论】:

  • 授予您存储图像的文件夹的权限。在 linux 中:命令是:sudo chmod -R 777 [你的文件夹名称]
  • 您忘记定义上传图片的路径$config['upload_path'] = './uploads/';
  • @Monty 我如何在 Windows 10 上授予权限?
  • @AhmedKhan 我应该在哪里定义它?在模型上是吗?
  • 在 windows 中无需授予权限。但您可以只检查文件夹属性并检查读写

标签: php codeigniter


【解决方案1】:
  'new_image' => $this->gallery_path . '/thumbs',

设置目标图像名称/路径。创建图像副本时,您将使用此首选项。 路径必须是相对或绝对的服务器路径,而不是 URL。

您将 new_image 设置为文件夹,而不是文件名。使它像 $this->gallery_path 。 '/thumbs/my_new_file.ext'

【讨论】:

  • 那个文件夹存在吗?
  • 是的,这里的问题是当我点击上传但我还没有选择图像,如果它有图像,一切正常
  • @HoàngTrungHiếu 正确。这不是许可的问题。用户尚未选择文件并单击上传按钮,因此他需要在上传文件时放置服务器端验证。
  • @AlankarMore 但我怎样才能显示一条消息:请选择图像而不是那种错误?
  • 可以放javascript验证。你可以使用 jquery.validation.min.js 来做同样的事情。但请确保您也可以进行服务器端验证。我已经在下面的答案中证明了这一点。
【解决方案2】:

你有高压终端接入吗?转到项目文件夹的根目录并在终端中键入以下内容 sudo chmod -R 755 *sudo chmod -R 777 * 第一个选项在某些项目中存在问题,因此第二个选项。

【讨论】:

    【解决方案3】:

    请勿在未检查错误的情况下直接上传图片。在上传之前检查文件中是否有任何错误。改变你的功能如下:

    function index()
    {
        $this->load->model('Gallery_model');
        if ($this->input->post('upload')) {
            $errors = $this->Gallery_model->do_upload();
            if (!$errors) {
                $data['images'] = $this->Gallery_model->get_images();
                $this->load->view('gallery_view', $data);
            } else {
                // your error displaying page.
            }
        }
    }
    
    function do_upload()
    {
        $error = array();
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 2000
        );
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $this->upload->do_upload();
            $image_data = $this->upload->data();
    
            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->gallery_path . '/thumbs',
                'maintain_ration' => true,
                'width' => 150,
                'height' => 100
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
        }
    
        return $error;
    }
    

    更多信息请阅读HERE

    【讨论】:

      【解决方案4】:

      先验证图片上传与否,再调整图片大小

      $result = array();
      
          if (!$this->upload->do_upload('userfile')) {
      
              $result['error'] = $this->upload->display_errors();
      
      
          }else {
      
             $image_data = $this->upload->data();
      
           $result['image_data']      =   $image_data;    
      
              $config = array(
              'source_image' => $image_data['full_path'],
              'new_image' => $this->gallery_path . '/thumbs',
              'maintain_ration' => true,
              'width' => 150,
              'height' => 100
              );
      
              $this->load->library('image_lib', $config);
              $this->image_lib->resize();
      
          }
      
      return $result;
      

      【讨论】:

        猜你喜欢
        • 2012-07-16
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2015-04-13
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多