我正在尝试将图像同时上传到文件夹和MySQL数据库中。但是当我将其插入数据库但未上传到文件夹时会发生什么。重要的是我希望它上传到文件夹名称uploads.please看看。
控制器:
Class AddPost extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('form');
$this->load->library('upload');
$this->load->model('add_post_model');
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('url', 'url', 'required');
/*$this->form_validation->set_rules('file', 'image', 'required');*/
$this->form_validation->set_rules('date', 'date', 'required');
$this->form_validation->set_rules('active', 'active', 'required');
$this->form_validation->set_rules('img_id', 'img_id', 'required');
$configUpload['upload_path'] = './uploads/'; #the folder placed in the root of project
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; #allowed types description
$configUpload['max_size'] = '0'; #max size
$configUpload['max_width'] = '0'; #max width
$configUpload['max_height'] = '0'; #max height
$configUpload['encrypt_name'] = true; #encrypt name of the uploaded file
$this->load->library('upload', $configUpload);
#init the upload class
if(!$this->upload->do_upload('uploadimage')){
$uploadedDetails = $this->upload->display_errors();
}else{
$uploadedDetails = $this->upload->data();
}
print_r($uploadedDetails);
$prefix = "http://";
if ($this->form_validation->run() == FALSE) {
$this->load->view('AddPostView');
}
else
{
$example = $this->add_post_model->GetImageId();
foreach ($example as $ex) {
echo $result = ($ex->ImgId + 1);
}
$data = array(
'ImgId' => $result,
'ImgPath' => $this->input->post('fileToUpload'),
'Url' => $prefix . $this->input->post('url'),
'Date' => $this->input->post('date'),
'Active' => $this->input->post('active')
);
$this->load->model('add_post_model');
$this->add_post_model->InsertAdd($data);
$data['message'] = 'Add Posted Successfully';
$this->load->view('AddPostView', $data);
}
}
}
?>
型号:
<?php
class add_post_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function InsertAdd($data)
{
$this->db->insert('Advertisements', $data);
}
function GetImageId()
{
/*$result = "SELECT img_id FROM advertisementnew ORDER BY img_id DESC LIMIT 1";*/
$this->db->select('*');
$this->db->select("ImgId");
$this->db->from('Advertisements');
$this->db->order_by("ImgId","desc");
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
}
}
?>
查看:
<html>
<body>
<head></head>
<?php echo form_open('AddPost'); ?>
<h1>Post Advertisements </h1><hr/>
<?php if (isset($message)) { ?>
<h3 style="color:green;">Add Posted successfully</h3><br>
<?php }
$date = date('Y-m-d H:i:s');
$status = '1';
?>
<form method="POST" enctype="multipart/form-data">
<label>URL:</label><input type="text" id="url" name="url" placeholder="Please Enter URL" value=""><br><br>
<label>File: </label><input type="file" name="fileToUpload" id="fileToUpload" value=""/><br><br>
<input type="hidden" id="active" name="active" value="<?php echo
<input type="hidden" id="date" name= "date" value="<?php echo $date; ?>">
<input type="hidden" id="img_id" name="img_id" value="<?php echo ($result)?>">$status; ?>">
<input type="submit" id="submit" name="submit" value="Submit"/>
</form>
</body>
答案 0 :(得分:0)
请放置<input type="file" name="userfile" id="fileToUpload" value=""/>
而不是<input type="file" name="fileToUpload" id="fileToUpload" value=""/>
,并确保将uploads
目录置于application
文件夹之外