워드프레스 wp-admin > 미디어 > 라이브러리 에서 delete permanently (영구삭제)를 할 경우, 서버에서도 삭제된다.
그러나 사이트에서 예를 들어 유저가 개인정보중에 자신의 사진을 변경한다고 하면, 기존의 사진과 새로운 사진이 모두 서버에 저장되어 서버 용량을 차지하게 된다.
그러므로 유저가 새로운 사진으로 업데이트 할 때는 기존의 사진은 삭제되도록 로직을 구성해야 한다.
text라면 큰 용량을 차지하지 않겠지만, 사진이나 영상들은 반드시 삭제를 하는 로직을 포함해야 한다. 다음은 그 예제이다.
// Check if user_login already exists in the database
$existing_entry = $wpdb->get_row($wpdb->prepare(“SELECT * FROM wp_users_tutor WHERE user_login = %s”, $user_login));
if ($existing_entry) {
// Delete old files if new files are uploaded
if (!empty($college[‘url’]) && !empty($existing_entry->college)) {
unlink(get_home_path() . str_replace(home_url(‘/’), ”, $existing_entry->college));
}
if (!empty($graduate[‘url’]) && !empty($existing_entry->graduate)) {
unlink(get_home_path() . str_replace(home_url(‘/’), ”, $existing_entry->graduate));
}
if (!empty($profile[‘url’]) && !empty($existing_entry->profile)) {
unlink(get_home_path() . str_replace(home_url(‘/’), ”, $existing_entry->profile));
}
이 코드에서 unlink
함수를 사용하여 파일을 삭제합니다. get_home_path()
와 str_replace(home_url('/'), '', $existing_entry->college)
를 사용하여 파일 경로를 가져옵니다. 새로운 파일이 업로드되면 기존 파일을 삭제하도록 설정하는 것이다.