user13474936
user13474936

Reputation:

Move directory and delete directory file

I am using PHP to do the move directory function. Now my problem is, I can move the file to another folder, but how original file can be deleted in the original folder? Because I using rename function just copy the file to another folder.

These two folder location dms/400_PENGURUSAN_KEWANGAN/123.pdf dms/500_PENGURUSAN_KEWANGAN/123.pdf

Below is my coding:


$file_path_2 = "dms/400_PENGURUSAN_KEWANGAN/123.pdf";
$new_file_path = "dms/500_PENGURUSAN_KEWANGAN/123.pdf";

rename("$file_path_2", "$new_file_path");

My result are dms/400_PENGURUSAN_KEWANGAN/123.pdf and dms/500_PENGURUSAN_KEWANGAN/123.pdf both got file.

My expected result are dms/400_PENGURUSAN_KEWANGAN and dms/500_PENGURUSAN_KEWANGAN/123.pdf

Upvotes: 0

Views: 173

Answers (1)

Simone Rossaini
Simone Rossaini

Reputation: 8162

First move file how you do, then delete folder and file like :

$filess = glob('dms/400_PENGURUSAN_KEWANGAN/*');
foreach($filess as $filesss){ // iterate files
if(is_file($filesss))
unlink($filesss); // delete file
}
if(is_dir('dms/400_PENGURUSAN_KEWANGAN')) { //check if dir exist
rmdir('dms/400_PENGURUSAN_KEWANGAN'); // delete dir
}

If you have only one file:

$filess = 'dms/400_PENGURUSAN_KEWANGAN/123.pdf';
unlink($filess); // delete file
if(is_dir('dms/400_PENGURUSAN_KEWANGAN')) { //check if dir exist
rmdir('dms/400_PENGURUSAN_KEWANGAN'); // delete dir
}

Upvotes: 1

Related Questions