CodeigniterのUploadClassを使用して、プロジェクト内のフォルダーに画像をアップロードしました。データベースには、画像のアップロード後に生成されたURLのみを保存するため、データベースの行を削除する場合は、画像も削除する必要があります。codeigniterでどうすればいいですか?
私はあなたの答えに感謝します。
CodeigniterのUploadClassを使用して、プロジェクト内のフォルダーに画像をアップロードしました。データベースには、画像のアップロード後に生成されたURLのみを保存するため、データベースの行を削除する場合は、画像も削除する必要があります。codeigniterでどうすればいいですか?
私はあなたの答えに感謝します。
モデルの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';
}
}
delete_row_from_db(); unlink('/path/to/file');
/path/to/file は実際のパスでなければなりません。
例:
あなたのフォルダがこのような場合 htp://example.com/uploads
$path = realpath (APPPATH . '../uploads');
APPPATH = アプリケーション フォルダへのパス。
その作業...
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'];
}
}
$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);
}