php 如何使用 Codeigniter 删除上传的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14900012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:09:12  来源:igfitidea点击:

How to delete uploaded files with Codeigniter?

phpcodeigniterupload

提问by user2065593

I used the Codeigniter's Upload Class to upload images to a folder in the project. In the database I only store the the url generated after upload the image, so when I want to delete a row in the db I also need to delete the image. How can I do it in codeigniter?

我使用 Codeigniter 的 Upload Class 将图像上传到项目中的文件夹。在数据库中我只存储上传图片后生成的url,所以当我想删除db中的一行时,我也需要删除图片。我怎样才能在 codeigniter 中做到这一点?

I will be grateful for your answers.

我将不胜感激您的回答。

回答by tim peterson

You can delete all the files in a given path, for example in the uploadsfolder, using this deleteFiles()function which could be in one of your models:

您可以uploads使用此deleteFiles()功能删除给定路径中的所有文件,例如文件夹中的所有文件,该功能可能位于您的模型之一中:

$path = $_SERVER['DOCUMENT_ROOT'].'/uploads/';

function deleteFiles($path){
    $files = glob($path.'*'); // get all file names
    foreach($files as $file){ // iterate files
      if(is_file($file))
        unlink($file); // delete file
        //echo $file.'file deleted';
    }   
}

回答by Mansoorkhan Cherupuzha

delete_row_from_db(); unlink('/path/to/file');

/path/to/file must be real path.

/path/to/file 必须是真实路径。

For eg :

例如:

if your folder is like this htp://example.com/uploads

如果你的文件夹是这样的 htp://example.com/uploads

$path = realpath(APPPATH . '../uploads');

$path = realpath(APPPATH .'../uploads');

APPPATH = path to the application folder.

APPPATH = 应用程序文件夹的路径。

Its working...

它的工作...

回答by Deblina

if(isset($_FILES['image']) && $_FILES['image']['name'] != '') {

if(isset($_FILES['image']) && $_FILES['image']['name'] != '') {

            $config['upload_path']   = './upload/image'; 
            $config['allowed_types'] = 'jpeg|jpg|png';
            $config['file_name']    = base64_encode("" . mt_rand());
            $this->load->library('upload', $config);

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

            if (!$this->upload->do_upload('image')) 
            {
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('msg', 'We had an error trying. Unable upload  image');
            } 
            else 
            { 
                $image_data = $this->upload->data();
                @unlink("./upload/image/".$_POST['prev_image']);
                $testData['image'] = $image_data['file_name'];
            }
        } 

回答by user3124761

$m_img_real= $_SERVER['DOCUMENT_ROOT'].'/images/shop/real_images/'.$data['settings']->shop_now;
$m_img_thumbs = $_SERVER['DOCUMENT_ROOT'].'/images/shop/thumbs/'.$data['settings']->shop_now;

if (file_exists($m_img_real) && file_exists($m_img_thumbs)) 
{
     unlink($m_img_real);
     unlink($m_img_thumbs);
}

回答by Nilesh Ahir

View:

看法:

<input type="file" name="new_file" data-required="1" class="" />
<input type="hidden" name="old_file" value="echo your old file name"/>
<input type="submit" name="submit"/>

Controller:

控制器:

function edit_image() {

 if(isset($_FILES['new_file']['name']) && $_FILES['new_file']['name'] != '') {

                move_uploaded_file($_FILES['new_file']['tmp_name'],'./public_html/banner/'.$_FILES['new_file']['name']);

                $upload = $_FILES['new_file']['name'];

                $name = $post['old_file'];
                @unlink("./public_html/banner/".$name);
            }
            else {

                $upload = $post['old_file'];
            }              

}

回答by Puwel

Try using delete_files('path')function offered by CI framework itself: https://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

尝试使用delete_files('path')CI 框架本身提供的功能:https: //ellislab.com/codeigniter/user-guide/helpers/file_helper.html

回答by Arvind Singh

$image_data = $this->upload->data();
unlink($image_data['full_path']);

This line $this->upload->data()will return many information about uploaded file. You can print information and work accordingly.

此行将$this->upload->data()返回有关上传文件的许多信息。您可以打印信息并进行相应的工作。