파일 및 경로 디렉토리 삭제

Sangyeong Je·2023년 1월 3일
0

php

목록 보기
1/1
public function DeleteFile($data=null,$path=null)
	{
		try
		{
			$file = $data;
			if ($file != null) 
			{
				throw new Exception('File name is empty');
			}
			$fileName = basename($file);
			$filePath = dirname($file);
			$logPath = $path;
			if (file_exists($file))
			{
				unlink($file);
				echo 'File deleted successfully';
				$this->StackLogs($logPath, $fileName, $this->GetIPaddress(), 'delete');
				if (count(scandir($filePath)) == 2) 
				{
					rmdir($filePath);
					echo ' and Directory deleted successfully';
				}
			}
			else
			{
				throw new Exception('File does not exist');
			}
		}
		catch (Exception $e)
		{
			echo 'Error deleting file: ' . $e->getMessage();
		}
	}

이 코드는 파일을 삭제하는 기능을 구현한 것입니다.
코드의 첫번째 부분에서는 전달받은 파일 이름이 존재하지 않을 경우 예외를 처리합니다.
그리고 그 파일의 이름과 경로를 추출합니다.
다음으로, 해당 파일이 존재하는지 확인합니다.
존재한다면, 그 파일을 삭제하고 그 경로의 파일이 없다면 그 경로도 삭제합니다.
그렇지 않을 경우, 예외를 처리합니다.
이 코드는 삭제된 파일과 삭제된 경로에 대한 정보를 출력하고,
삭제 작업에 대한 정보를 로그로 저장합니다.

0개의 댓글