Hello! Again, the question is: unnecessary files are collected in my avatars folder, say, a user uploaded a photo, he didn’t like it, uploaded another one. The first photo is unnecessary and takes up space. Of course, you can delete it manually, but if there are many such users, this becomes impossible, especially since the names are all cached. Is there a php script that would compare the names of the photos in the avatars folder with the names of the photos that are recorded in the database and delete the unused avatars from the folder?

  • There is but it needs to be written) - Palmervan

2 answers 2

When updating a photo (the user has uploaded a new photo) we remove the address of the old image from the database, delete it via unlink($file) (be careful, the address to the file in the file system may be different from the address to the file in the address line), the address of the new photo is written in the database.

  • Thank you, everything is as simple as ever) - Regina
  • I get an error: Warning: unlink (avatars /): Permission denied in z: \ home \ localhost \ www \ spravmrakovo nov \ www \ spravmrakovo.ru \ update.php on line 59. It’s written that it’s denied, maybe it’s not right ? - Regina
  • one
    @Regina, you clearly see in the error that the unlink (avatars /) folder is specified, but there is no file. - lampa
  • Similarly, I myself just now understood, I always make silly mistakes). - Regina
  • one
    Everything works, thank you! - Regina
 function clear() { if (file_exists('/cache/')) foreach (glob('/cache/*') as $file) unlink($file);}