【发布时间】: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