查看/ image.php:
<form action="upload.php" method="post">
<h3>Image Upload Form</h3>
<input type="file" name="pic" tabindex="2" required>
<input type="text" name="alt" placeholder="Image Alt Text" tabindex="1"
required>
<button type="submit" id="img-submit" data-
submit="Sending">Submit</button>
</form>
控制器/ upload.php的:
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
}
function image()
{
$this->load->view('pages/image');
}
function upload()
{
$data = array(
'image_url' => $this->input->$_FILES['pic']['name'],
'alt' => $this->input->post('alt')
)
}
模型/ upload_m.php:
<?php
class pages_m extends CI_Model {
function __construct()
{
parent::__construct();
}
function upload($data)
{
return $this->db->insert('image',$data);
}
}
?>
当我尝试通过localhost/codeigniter/index.php/pages/image
访问该页面时,它会显示error 404 not found
。如果有人可以检查我的代码是否可能出错,我将不胜感激?提前谢谢!
答案 0 :(得分:2)
我认为您忘记在表单标记中写入enctype。 使用此代码
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Image Upload Form</h3>
<input type="file" name="pic" tabindex="2" required>
<input type="text" name="alt" placeholder="Image Alt Text" tabindex="1"
required>
<button type="submit" id="img-submit" data-
submit="Sending">Submit</button>
</form>
答案 1 :(得分:0)
表单attrtibute enctype="multipart/form-data"
丢失,当您使用具有文件上载控件的表单时,这是必需的。有关详细信息https://www.w3schools.com/tags/att_form_enctype.asp。您还可以使用CI帮助程序功能来声明表单标记form_open_multipart
表单操作网址不正确,说明您收到404错误的原因。有关详细信息,请https://www.codeigniter.com/userguide3/tutorial/static_pages.html
您没有包含CI上传库,也没有按照CI教程中提到的基本步骤进行操作。请在此处找到上传教程https://www.codeigniter.com/userguide3/libraries/file_uploading.html
欢迎任何疑问。
答案 2 :(得分:0)
要做的第一件事应该是纠正你的表格:
<form action="upload.php" method="post" enctype="multipart/form-data">
然后你应该将控制器代码包装在其中并注意拼写错误,因为这些是区分大小写的
(使用大写字母重命名您的控制器作为第一个字母: upload.php的):
class Upload extends CI_Controller {
}
然后让我们转到您的模型(Pages_Model.php):
class Page_Model extends CI_Model {
}
此外,我看不到您在控制器中加载模型的位置。您必须在控制器的构造函数中或在使用模型函数所需的函数中执行类似的操作:
$this->load->model('Page_Model');
当你想使用模型中的函数时,你必须这样做:
$this->Page_Model->function_name($data);
您可以随时查看codeigniter的网站以获取文档。它写得很好,易于理解。干杯!
LE:我忘记了上传。
public 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('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
有用链接:https://www.codeigniter.com/userguide3/libraries/file_uploading.html
这是一个示例函数。在您的代码中,您没有上传任何内容。您的数据库无法保存文件。您将它用于文件名(在本例中)。
答案 3 :(得分:0)
在控制器中添加此功能并尝试。
public function doupload()
{
$upload_path="https://localhost/project/profile"
$uid='10'; //creare seperate folder for each user
$upPath=upload_path."/".$uid;
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config = array(
'upload_path' => $upPath,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('pic'))
{
$data['imageError'] = $this->upload->display_errors();
}
else
{
$imageDetailArray = $this->upload->data();
$image = $imageDetailArray['file_name'];
}
}