图像上载在Codeigniter中不起作用



我在本地服务器上上传图像时遇到问题。

我刚开始学习Codeigniter引用https://www.phptpoint.com/Codeigniter-upload-file-image/和Codeigniter文档。

我遵循了每一步,但当我点击上传按钮时,我无法打开页面。safari表示"无法打开页面"错误。

我应该在代码中犯了一些错误。

有人找到了吗?

附言:我甚至在没有附加图像时出错。也许这个错误不是关于上传图片?

<?php 
class ImageUpload_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//load Helper for Form
$this->load->helper('url'); 
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('imageupload_form');
}
public function upload() 
{
$new_name = date('ymd') . time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
$config['file_name']  = $new_name;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('profile_pic')) 
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('imageupload_form', $error);
} 
else 
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('imageupload_success', $data);
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Image in Codeigniter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
// I'm not sure but referenced website used "@" right before $error so I kept using. when omitted it I got errors
<?php echo @$error; ?> 
<?php echo form_open_multipart('imageupload_controller/upload');?>
<?php echo "<input type='file' name='profile_pic' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>";?>
</body>
</html>

删除这些行

if (!file_exists($path)) {
mkdir($path, 0755, true);
}

我没有看到它的任何使用案例。

我更新了代码中的几行。请运行下面给出的命令,使您的目录具有可写权限。

sudo chown www-data:www-data /var/www/[new directory]

添加变量名$path = '/var/www/html/ur_project_directory/uploads;在功能启动后的第一行。并用您的目录名更改ur_project_directory

这是测试后的工作代码。

这是视图文件。

<!DOCTYPE html>
<html>
<head>
<title>Upload Image in Codeigniter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php echo @$error; ?> 
<?php echo form_open_multipart('test/upload');?>
<?php echo "<input type='file' name='profile_pic' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>";?>
<?php if ($this->session->flashdata('status')) { ?>
	<label><?php  echo $this->session->flashdata('status'); ?></label>
<?php } ?>
</body>
</html>

这是您的控制器文件

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MX_Controller {
function __construct()
{
parent::__construct();
//load Helper for Form
$this->load->helper('url'); 
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('test-view');
}
public function upload() 
{
$new_name = date('ymd') . time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
$config['file_name']  = $new_name;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('profile_pic')) 
{
$data = $this->upload->display_errors();

$this->session->set_flashdata('status', $data);
$this->load->view('test-view', $data);
} 
else 
{
$data = 'Your file uploaded sucessfully.';
$this->session->set_flashdata('status', $data);
$this->load->view('test-view', $data);
}
}
}
?>

相关内容

  • 没有找到相关文章

最新更新