5

CodeigniterのUploadClassを使用して、プロジェクト内のフォルダーに画像をアップロードしました。データベースには、画像のアップロード後に生成されたURLのみを保存するため、データベースの行を削除する場合は、画像も削除する必要があります。codeigniterでどうすればいいですか?

私はあなたの答えに感謝します。

4

7 に答える 7

12

モデルの1つにある可能性のあるこの関数を使用して、特定のパス(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';
    }   
}
于 2013-02-16T04:07:02.887 に答える
4
delete_row_from_db(); unlink('/path/to/file');

/path/to/file は実際のパスでなければなりません。

例:

あなたのフォルダがこのような場合 htp://example.com/uploads

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

APPPATH = アプリケーション フォルダへのパス。

その作業...

于 2013-02-16T03:59:36.950 に答える
1

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'];
            }
        } 
于 2013-02-16T04:52:48.170 に答える
0
$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);
}
于 2013-12-21T06:28:04.910 に答える