Hi,

I will try describe the algorithm.

I have a website, with articles. Administrator can upload image files and then put them into the article.
Sometimes it can happen that admin uploads file but does not save the article and closes the browser. So the file isn’t needed anymore - it is a trash in the server.
To solve this - I made a table, which contains filenames together with article id’s. When admin saves the article - the filenamaes of images to that article are also added to database table.

So now we can make a function which deletes files that are not in the database - by checking the table for a file name, and if there is not such filename in table, it removes it from server.

Now the problem - somehow after some time I found that many of the file names are gone from the database table, which are actually needed. So the function which cleans unneeded files cleans also the needed ones, because it does not find them in a table.

But I cannot find when do they dissapear from the table or maybe they are not inserted at all. I try myself to write artile, upload files, remove files, edit article, edit and remove files and everything works well while I am trying. I also tried searching for the table name in all CI instalation files, and only there are 3 files where that table name is used, so I checked the code and tried again to make that code execute to make all situations and still cannot find where it could not work.

So don’t know what to do, maybe I miss something. What to do in such situation? leave everything as it is and wait until something goes wrong?

I cannot leave the clean trash function, because I am not sure if everything will work. And yet I will have to manually insert the needed filenames into the filenames table with the article ids. Because there is no way to know for an algorithm which filename is for what article.

First thing that you might want to do is to log all the pertinent info when you delete anything. The second thing that you might want to do (at least for a while) is to do a soft-delete instead. Just mark the record as deleted or save a copy under a special "deleted" key or move it to another "deleted record" table.

With both of these in place you will be able to investigate what is going on and you won't permanently lose any data.

Thank you very much for these ideas. So the only thing is I could do everything as you said and after some time just check the logs where those deletes come from.

Edit:

I found one thing that is not correct, but I still don't believe that it caused the loss of like half filenames in database:

function delete_picture_edit_ajax()
{
    	
   $this->db->where('filename',$this->input->post('filename'))->delete('naujienu_nuotraukos'); 	   
   if($this->membership->is_logged_in())
   {
   	 unlink('./uploads/naujienos/'.$this->input->post('filename'));
   }
   else echo 'not_admin';
}

it first removes the filename from db, then checks if the admin is connected and if it is, then removes the file in server. So it means that there had to be tryings to delete when admin was not connected which I don't believe. Admin would not even want to delete that many files. Unless some hacker could run such function, but also I don't believe :) how could he now that he needs to run delete_picture_edit_ajax with filename in $_POST ?

But I corrected that - now it checks if admin is logged in before those statemenets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.