0

I want to make a function to delete a file in my folder through php. I've written the code based on suggestion from here, but eventually it is not deleting the file I intended to remove. It's removing other file instead. Here's the code :

$file_name = $this->input->post('file_name'); //getting file name from view

$path_gse = realpath('file_upload/station_report/gse_serviceability/'.$file_name);
if(is_writable($path_gse)) {
    unlink($path_gse);
}

In $file_name variable I pass it from view with ajax, I've tested it and it did return the correct file name. I really have no idea why this function delete the file that I don't intended to remove.

UPDATE

Here's the files in folder before I delete, file before delete

Then I delete a row number 3 from html table, table stackoverflow

It suppose to delete a file named crew_conden-1801-Local_Time ver download tgl 18 Jan 18, but here's what happened,file after delete

It deleted OTP Dashboard development file instead of crew_conden-1801-Local_Time ver download tgl 18 Jan 18

aan
  • 29
  • 9
  • 5
    Highly relevant information regarding your issue is things like, your folder structure and file layout, what is sent in the `$file_name` variable and which file is wrongfully deleted in your eyes. – inquam Oct 15 '18 at 08:39
  • You don't need to use real path to delete files! – Salim Ibrohimi Oct 15 '18 at 08:41
  • 1
    I finding it hard to believe that the issue is a bug in PHP, pretty sure there's something wrong with your code after all, but you didn't provide enough details to actually know what's going on – Alon Eitan Oct 15 '18 at 08:42
  • `echo $path_gse` to check for the path – Zain Farooq Oct 15 '18 at 08:47
  • Are you really running your script in the folder just below file_upload? I would recommend you to use the full path to the file, and not a current path relative path. (e.g if file_upload is in /var/www make sure to start your path with that. So in your code the realpath row would be $path_gse = realpath('/var/www/file_upload/station_report/gse_serviceability/'.$file_name); – Virre Oct 15 '18 at 08:56
  • @inquam I've updated my question, please take a look. – aan Oct 15 '18 at 09:01
  • @AlonEitan I've updated my question, please take a look. – aan Oct 15 '18 at 09:04
  • 2
    Sounds like your “Delete” buttons are not be sending the correct file name to begin with. You likely made a mistake creating them, or the form/forms they are in, so that only the name of the last file is ever send. – misorude Oct 15 '18 at 09:06
  • @aan As misorude wrote, try to inspect the delete button elements and see what value did you set to them – Alon Eitan Oct 15 '18 at 09:20
  • Have you done a check of the variable `$file_name` when you receive it so it contains what you expect on the backend? Do you encode the name when sending it? – inquam Oct 15 '18 at 09:49

0 Answers0