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

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

上傳圖像文件時添加時間戳不起作用

上傳圖像文件時添加時間戳不起作用

PHP
白板的微信 2023-05-26 16:23:53
我正在開發一個廣告網站,任何人都可以上傳 3 張圖片。因此,我正在編碼以通過在文件名前添加時間戳來上傳這 3 張圖像,使它們獨一無二。我使用 codeigniter 框架并附上下面的代碼。當我提交表單數據時,本地主機服務器顯示圖像已正確保存,并帶有一些圖像文件名的代碼信息。但問題是沒有以該名稱保存在相關圖像文件夾中的圖像,我無法在下一個預覽廣告頁面中檢索該圖像。我不太了解 php 或 codeignitor。非常感謝您的幫助。        $config['upload_path'] = './assets/images/adsimages';        $config['allowed_types']        = 'gif|jpg|png';        $config['max_size']             = 5120;        $this->load->library('upload',$config);        $this->upload->initialize($config);        if (!$this->upload->do_upload()){            $errors = array('error' => $this->upload->display_errors());            $post_image = 'no_image.jpg';        }        else {            $data = array('upload_data' => $this->upload->data());            $post_image1 = time().$_FILES['userfile1']['name'];            $post_image2 = time().$_FILES['userfile2']['name'];            $post_image3 = time().$_FILES['userfile3']['name'];        }                 $result = $this->post_model->adregister($post_image1,$post_image2,$post_image3);
查看完整描述

3 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

您可以在上傳庫的配置中將時間附加到“文件名”

$config['file_name'] = time().$_FILES['userfile1']['name'];

或者,如果您想要所有文件的唯一名稱,只需添加

$config['encrypt_name'] = TRUE;

然后

$this->load->library('upload', $config);


查看完整回答
反對 回復 2023-05-26
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

試試這個:-


$path = pathinfo($_FILES["userfile1"]["name"]);

$image_path = $path['filename'].'_'.time().'.'.$path['extension'];


查看完整回答
反對 回復 2023-05-26
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

我已經為您的代碼編寫了一個可能的解決方案。您還沒有共享完整的代碼,因此您必須自己填補空白,并且可能會在這里或那里進行一些更改;必要時會提及評論??纯此欠駥δ阌袔椭?/p>


public function your_function_name(){

    // your-code

    // your-code


    // check if file is uploaded in field1

    if(!empty($_FILES['userfile1']['name'])){ 


        // call function to upload file

        $userfile1 = $this->upload_file('userfile1'); 

    }


    // check if file is uploaded in field2

    if(!empty($_FILES['userfile2']['name'])){ 


        $userfile2 = $this->upload_file('userfile2');

    }


    // check if file is uploaded in field3

    if(!empty($_FILES['userfile3']['name'])){ 


        $userfile3 = $this->upload_file('userfile3');

    }


    $result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);

}


// function to upload file

function upload_file($filename){


    $config['file_name']     = time().$_FILES[$filename]['name']; // append time to filename

    $config['upload_path']   = './assets/images/adsimages';

    $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';

    $config['max_size']      = 5120;


    $this->load->library('upload', $config);

    $this->upload->initialize($config);


    $uploaded = $this->upload->do_upload($filename);


    if ( ! $uploaded ){


        $error = array('error' => $this->upload->display_errors());

        $file  = 'no_image.jpg'; // default file


    }else{


        $upload_data = $this->upload->data();

        $file        = $upload_data['file_name']; // uploaded file name

    }


    return $file; // return filename

}


查看完整回答
反對 回復 2023-05-26
  • 3 回答
  • 0 關注
  • 237 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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