[英]CodeIgniter: Image is not uploading.
gallery.php-視圖
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>CI Gallery</title>
</head>
<body>
<div id="upload">
<?php
$this->load->helper("form");
echo form_open_multipart('gallery/up');
?>
<input type="file" name="file">
<?php
echo form_submit('upload','Upload');
echo form_close();
?>
</div>
</body>
</html>
gallery.php控制器
<?php
class Gallery extends CI_Controller {
function index()
{
$this->load->view('gallery');
}
function up()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload',$config);
$this->upload->do_upload('file');
$this->load->view('gallery');
}
}
這些是我的編碼文件。 這里沒有上傳文件。 有人請幫忙。 我感覺庫上傳失敗,因此無法正常工作。 如果那是問題,那么如何解決。
在您的html中添加表單標簽
<form enctype="multipart/form-data" name="" action=""></form>
您的代碼似乎很好,沒有錯誤。 請嘗試以下代碼,檢查是否上傳了圖片,是否是路徑問題? 復制下面的代碼並放在上面
"$this->load->view('gallery');"
這在您的控制器的向上功能中。
if (! $this->upload->do_upload())
{
$error = array('error' = $this->upload->display_errors());
// uploading failed. $error will holds the errors.
}
else
{
$data = array('upload_data' => $this->upload->data());
// uploading successfull, now do your further actions
}
您可以編寫上傳圖像服務器端代碼
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error); //debug it here
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
問題在於要上傳圖像的文件夾沒有寫權限,因此我將該文件夾設為可寫狀態,並且現在可以正常工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.