Php Can';t上传图像和其余的输入数据
我对CodeIgniter非常陌生,所以我只能利用框架所能提供的某些方面。在本例中,我希望使用户能够上载一个包含其其余信息的图像。我尝试将我所创建的内容中的逻辑与我在这里找到的教程和信息相结合,但没有成功。根据该代码当前的状态,它会将所有信息上传到服务器,但不会上传与用户输入的图像有关的任何信息。所以,我想我做了一些致命的错误。任何建议都将不胜感激Php Can';t上传图像和其余的输入数据,php,codeigniter,upload,Php,Codeigniter,Upload,我对CodeIgniter非常陌生,所以我只能利用框架所能提供的某些方面。在本例中,我希望使用户能够上载一个包含其其余信息的图像。我尝试将我所创建的内容中的逻辑与我在这里找到的教程和信息相结合,但没有成功。根据该代码当前的状态,它会将所有信息上传到服务器,但不会上传与用户输入的图像有关的任何信息。所以,我想我做了一些致命的错误。任何建议都将不胜感激 function adduser(){ $config['upload_path'] = './uploads';
function adduser(){
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$uinfo = $this->upload->data();
$file = array(
'img_name' => $uinfo['raw_name'],
'ext' => $uinfo['file_ext']
);
$fullfile = $file['img_name'].$file['file_ext'];
$data = array(
'userid' => $this->session->userdata('userid'),
'name' => $this->input->post('name'),
'year' => $this->input->post('year'),
'title' => $this->input->post('title'),
'image' => $fullfile
);
$this->db->insert('user', $data);
redirect('user/index');
}
请尝试以下方法
public function adduser(){
{
//This is the directory to which we upload our image
//full permission is required for this folder
//FCPATH is a constant defined in index.php - its the relative path to project root
$config['upload_path'] = FCPATH.'uploads/';
//Validation - Only gif jpg png extensions are allowed
$config['allowed_types'] = 'gif|jpg|png';
//unique file name
$file_name = time();
$config['file_name'] = $file_name;
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
//checking file upload success
if ( ! $this->upload->do_upload('image')) //here image is your file input name
{
//error in file uploading
$message = $this->upload->display_errors();
echo "<pre>";
print_r($message);
exit;
}
else
{
//successfully uploaded the image
$message = 'file uploaded successfully';
//getting full file upload information
$result = $this->upload->data();
$data = array(
'userid' => $this->session->userdata('userid'),
'name' => $this->input->post('name'),
'year' => $this->input->post('year'),
'title' => $this->input->post('title'),
'image' => $result['file_name']
);
$this->db->insert('user', $data);
}
redirect('user/index');
}
公共函数adduser(){
{
//这是我们上传图片的目录
//此文件夹需要完全权限
//FCPATH是index.php中定义的常量,它是指向项目根的相对路径
$config['upload_path']=FCPATH.uploads/;
//验证-仅允许gif jpg png扩展
$config['allowed_types']='gif | jpg | png';
//唯一文件名
$file_name=time();
$config['file\u name']=$file\u name;
$config['max_width']='1024';
$config['max_height']='1024';
$this->load->library('upload',$config);
//正在检查文件上载成功
如果(!$this->upload->do_upload('image')//这里image是您的文件输入名
{
//文件上载时出错
$message=$this->upload->display_errors();
回声“;
打印($message);
出口
}
其他的
{
//已成功上载图像
$message='文件上传成功';
//获取完整文件上载信息
$result=$this->upload->data();
$data=数组(
'userid'=>$this->session->userdata('userid'),
'name'=>this->input->post('name'),
“年”=>$this->input->post('year'),
'title'=>this->input->post('title'),
'image'=>$result['file_name']
);
$this->db->insert('user',$data);
}
重定向(“用户/索引”);
}
在上面我使用了“image”作为文件输入字段名如果您还没有这样做,请使用CI的错误报告,并查看是否有任何结果。@您错过了codeigniter上载过程的唯一原因。应该使用
$this->upload->do\u upload()
。有关详细信息,请查看链接。谢谢!这非常有效。现在我可以继续使用图像的删除功能。
public function adduser(){
{
//This is the directory to which we upload our image
//full permission is required for this folder
//FCPATH is a constant defined in index.php - its the relative path to project root
$config['upload_path'] = FCPATH.'uploads/';
//Validation - Only gif jpg png extensions are allowed
$config['allowed_types'] = 'gif|jpg|png';
//unique file name
$file_name = time();
$config['file_name'] = $file_name;
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
//checking file upload success
if ( ! $this->upload->do_upload('image')) //here image is your file input name
{
//error in file uploading
$message = $this->upload->display_errors();
echo "<pre>";
print_r($message);
exit;
}
else
{
//successfully uploaded the image
$message = 'file uploaded successfully';
//getting full file upload information
$result = $this->upload->data();
$data = array(
'userid' => $this->session->userdata('userid'),
'name' => $this->input->post('name'),
'year' => $this->input->post('year'),
'title' => $this->input->post('title'),
'image' => $result['file_name']
);
$this->db->insert('user', $data);
}
redirect('user/index');
}