【问题标题】:Secure upload and show image or file in codeigniter 3在 codeigniter 3 中安全上传和显示图像或文件
【发布时间】:2016-01-30 16:23:14
【问题描述】:

我想在我的 codeigniter 3 中上传图片,我想向我的用户展示上传的图片(这是在注册级别,用户正在输入他的个人资料数据) 我应该把上传的图片给他看。 我读过这个:

将它移到 public_html 之外是个好主意,也可以尝试 重命名文件并添加扩展名。

还有一个:

不要将上传的文件移动到可通过 URL 访问的目录

但我不知道如何显示不是可从 URL 访问的目录的图片! . 我不知道使用codeigniter上传类的安全性对我来说真的很重要,我不知道我应该做什么样的安全性其他操作 这是我的控制器:

public function do_resize($img_name ,$image_original_width , $image_original_height   )
{

    // $nesbat = $image_original_height  / $image_original_width ;

    $config_manip = array(
    'image_library' => 'gd2',
    'source_image' => '../uploads/'.$img_name,
    'new_image' => '../uploads/'.$img_name,
    'maintain_ratio' => TRUE,
    'create_thumb' => TRUE,
    'thumb_marker' => '_thumb',
    'width' => 150,
    'height' => 150
    );
    $this->load->library('image_lib', $config_manip);
    if (!$this->image_lib->resize()) {
        // echo $this->image_lib->display_errors();
        return false ;
    }
    else
    {
        return true ;
    }
    // clear //
    $this->image_lib->clear();

}

function do_upload()
{

    $file_name = $this->input->post("file_name") ;

    $config['upload_path'] = '../uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['file_name']  = $file_name;


    // delete if .gif image exists before

    if ( is_file('./uploads/'.$file_name.".gif")   )
    {
        unlink("./uploads/".$file_name.".gif"); 
        unlink("./uploads/".$file_name."_thumb.gif"); 
    }



    // delete if .gif image exists before

    if ( is_file('./uploads/'.$file_name.".jpg")   )
    {
        unlink("./uploads/".$file_name.".jpg"); 
        unlink("./uploads/".$file_name."_thumb.jpg"); 
    }


    // delete if .gif image exists before

    if ( is_file('./uploads/'.$file_name.".png")   )
    {
        unlink("./uploads/".$file_name.".png"); 
        unlink("./uploads/".$file_name."_thumb.png"); 
    }


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



    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        echo "<div id='upload_status'>fail</div>";
        echo "<div id='error_mesage'>".$this->upload->display_errors()."</div>";
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $upload_data = $this->upload->data(); 

        $uploaded_file_name =   $upload_data['file_name'];

        $resize = $this->do_resize($uploaded_file_name  , $upload_data['image_width'] , $upload_data['image_height'] ) ;
        if ($resize == true ) 
        {
            echo "<div id='upload_status'>success</div>";
            echo "<div id='uploaded_image_link'  >".$upload_data['file_name']."</div> ";
            $thumb_link = str_replace($file_name,$file_name."_thumb",$upload_data['file_name']);
            echo "<div id='uploaded_image_thumb_link'  >".$thumb_link."</div> ";
        }
        //if $resize == true , nabashe -> uploade koli fail eleam mishe ta dobare anjam beshe
        else 
        {
            echo "<div id='upload_status'>fail</div>";
        }


    }
}

【问题讨论】:

    标签: php codeigniter security image-uploading


    【解决方案1】:

    图像通常是公共资产,但您可以通过几种方式保护它们。

    1. 将 index.html 或 index.php 文件放入您的图像目录中。
    2. Turn off directory listing in your .htaccess file
    3. 重写文件名(会混淆原始名称)

    要在上传图片后查看图片,需要 AJAX 或页面刷新。页面刷新更容易编写代码,只需上传文件并在前一页上显示该文件。

    您可以保护文件夹以确保特定页面有权显示图像。这会使事情变得更加复杂,但使用某种资源访问系统可能会帮助您实现这一目标。

    一旦页面被加载 - 该图像将可供该用户使用并且一旦下载到那里。

    我不确定您要保护的具体内容(个人资料图片、成人内容..),但图像(如 CSS 文件)是公共资产。

    丰富

    【讨论】:

      【解决方案2】:

      通过这种方式,我们可以防止来自其他服务器的Image调用。

      防止图片盗链 (IMP)
      - 图片盗链是在我们的网站上使用他人的图片 URL 并使用他们的带宽的过程/技术。为了避免这个谜团,我们可以通过在 htaccess 中添加以下行来阻止外部服务器的访问。

      RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
      RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
      

      【讨论】:

        【解决方案3】:

        只需创建一个空白 index.html 文件并将 index.html 文件放在除应用程序/系统文件夹之外的所有其他公共文件夹(他们已经拥有它)。 这是一种简单的安全技术,可限制查看者查看您在公共文件夹中的文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-16
          • 1970-01-01
          • 2020-03-02
          • 2020-05-08
          • 2017-01-16
          • 1970-01-01
          相关资源
          最近更新 更多