亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我是 php 新手,我需要用隨機名稱上傳文件

我是 php 新手,我需要用隨機名稱上傳文件

PHP
慕容森 2021-06-15 21:06:47
我是 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);                                   }
查看完整描述

3 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

$pic_file1 = $this->input->post('pic_file');


        $config['upload_path']          = './uploads/';

        $config['allowed_types']        = 'gif|jpg|png';

        $config['max_size']             = 1000;

        //$config['encrypt_name'] = TRUE;

        // $config['overwrite'] = FALSE; 

        $config['file_name'] =  time();          


        $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);                       


        }


查看完整回答
反對 回復 2021-06-19
?
catspeake

TA貢獻1111條經驗 獲得超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,請告訴我我會提供。



查看完整回答
反對 回復 2021-06-19
  • 3 回答
  • 0 關注
  • 191 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號