php - 图像不保存到临时文件夹只保存到数据库
问题描述
我希望上传保存到我的项目目录中的 upload/images 文件夹,但事实并非如此。上传保存到数据库目录时,upload/images 文件夹为空
public function addArticleForm(){
//check whether user upload picture
if(!empty($_FILES['image_path']['upload_path'])){
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|';
$config['file_name'] = $_FILES['image_path']['upload_path'];
//load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('image_path')){
$uploadData = $this->upload->data();
$image_path = $uploadData['file_name'];
}else{
$image_path = '';
}
}
解决方案
试试这个为图片上传设置动态功能
public function upload_images($path,$filename){
$config = array(
'upload_path' => $path,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "204800", // 20 mb
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload($filename)){
$upload_data = $this->upload->data();
$datas = array('file_name' => $upload_data['file_name'], 'file_size'=>$upload_data['file_size'], 'file_type'=>$upload_data['file_type']);
$file_name = $upload_data['file_name'];
$file_size = $upload_data['file_size'];
$file_type = $upload_data['file_type'];
} else {
$datas = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_msg',$datas['error']);
}
return $datas;
}
并在另一个函数中使用上述函数,比如
public function Add_Customer_Profiles(){
if($this->session->userdata('userid')!="" || null!==$this->session->userdata('userid')){
$custno = $this->input->post('custno',TRUE);
$fname = $this->input->post('fname',TRUE);
$lname = $this->input->post('lname',TRUE);
$email = $this->input->post('email',TRUE);
$mobile = $this->input->post('mobile',TRUE);
$path = './assets/profile_data/';
$img_name = 'customer_img'; // Your file input name
$file_detail = $this->upload_images($path,$img_name);
if(!isset($file_detail['error'])){
//Your Code here
}
}
推荐阅读
- python - 如何使用 csv 中的 matplotlib 创建饼图
- pandas - 转置列并将列名用作另一行中的值
- c# - 解析复杂的 XML 并提取值
- c# - 为什么尝试在编辑器窗口中进行垂直滚动时出现异常 InvalidOperationException: Stack empty.?
- android - 使用 ffmpeg 为视频 Android 加水印
- pdf - Weasyprint:在base64中创建pdf而不保存它
- node.js - 在节点中创建计划函数
- java - 一个应用程序怎么能让设备几乎完全崩溃?
- java - 数字格式异常:空
- kubernetes - 如何使用 Kubernetespod 修复 ImagePullBackOff?