我是php的新手,我需要上传具有分配给该文件的随机名称的文件,并以随机名称存储该文件,以上传文件夹并将该随机名称存储到mysql数据库中。
$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);
}
答案 0 :(得分:1)
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);
}
答案 1 :(得分:0)
以上代码与任何框架都相关。如果您是php新手,请尝试使用下面提到的简单代码。
$target_dir = "/var/www/html/uploads/"; // this is base path
$imageFileType = strtolower(pathinfo(basename($_FILES["pic_file"]["name"]),PATHINFO_EXTENSION));
$filename = time().$imageFileType; //save this file name to database $filename
$target_file = $target_dir.$filename
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["pic_file"]["tmp_name"]);
if($check !== false) {
if (move_uploaded_file($_FILES["pic_file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["pic_file"]["name"]). " has been uploaded on : ".$target_file;
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "File is not an image.";
}
}
如果您还需要HTML,请告诉我我会提供。
答案 2 :(得分:0)
$ pic_file1 = $ this-> input-> post('pic_file');
ScrollView