【问题标题】:adding timestamp while uploading image file is not working上传图像文件时添加时间戳不起作用
【发布时间】:2020-10-04 00:08:14
【问题描述】:

我正在开发一个任何人都可以上传 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);

【问题讨论】:

  • 1.当您将文件保存在数据库中时,您正在更改文件名,但上传的文件名没有更改。 2. 上传 3 张图片时,您似乎只上传了 1 张图片。
  • 是的。我该如何解决。
  • 你能帮我解决吗
  • 我添加了一个答案@Steve Daran
  • @sauhardnc 非常感谢。有用。我尝试了很多次来解决它,但无法做到。您的代码工作正常。再次感谢你

标签: php codeigniter file-upload timestamp unique


【解决方案1】:

试试这个:-

$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];

【讨论】:

    【解决方案2】:

    我已经为您的代码编写了一个可能的解决方案。你还没有分享你的完整代码,所以你必须自己填补空白,也许在这里和那里做一些改变;必要时提及评论。看看对你有没有帮助。

    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
    }
    

    【讨论】:

      【解决方案3】:

      您可以在上传库的配置中将时间附加到“文件名”

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

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

      $config['encrypt_name'] = TRUE;
      

      然后

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-07
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 2019-11-14
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        相关资源
        最近更新 更多