php - i am new in php i need to upload file with random name
问题描述
I am new in php i need to upload file with random name assigning to the file and store that file with random name to upload folder and store that random name into mysql database.
$pic_file1 = $this->input->post('pic_file');
$pic_file1 = str_replace( "\\", '/', $pic_file1);
$filename = time().basename($pic_file1);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
//$config['encrypt_name'] = TRUE;
// $config['overwrite'] = FALSE;
$config['file_name'] = $filename;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('pic_file'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
// print_r($data);
}
解决方案
In your code just uncomment $config['encrypt_name'] = TRUE;
then automatically your file name store in random name formate or jsut copy below code
$pic_file1 = $this->input->post('pic_file');
$pic_file1 = str_replace( "\\", '/', $pic_file1);
$filename = time().basename($pic_file1);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['encrypt_name'] = TRUE;
// $config['overwrite'] = FALSE;
$config['file_name'] = $filename;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('pic_file'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
// print_r($data);
}
推荐阅读
- ios - 如何检索 UILabel 的属性字体?
- azure-functions - 部署 Azure Function 时向 Function.Json 添加绑定
- c++ - 模板化功能仅适用于某些类型
- html - 将新的 jsx 渲染加载到现有的 Div
- typescript - 如何将联合类型转换为元组类型
- java - 可以使用 Camel Servlet 的多个 servlet?
- python - 为 numpy 数组的特定列设置最大值
- ros - 使用覆盆子和 ROS 控制无人机 dji m100 的安全性
- html - 内部 Google 字体 CSS
- r - R中的Wilcox.test - 没有足够的x观察