php code doesn't execute properly on server




I have created this code, it checks if file has to be deleted or not, i have run it many times on localhost and it executes 10/10 correctly, if it find file that has to be deleted it deletes it. I also have this very same code on server (using shared hosting) it also executes right there but rate would be 9/10 times, that 1 time it doesn't execute right is that it doesn't delete file from given path (array_map), why is that so? Is there some settings i have to update on server?

function deleteExpired(){
		$path = $_SERVER['DOCUMENT_ROOT'];
		$sql = "SELECT * FROM uploads";
		$result = $conn->query($sql);
		while($row = $result->fetch_assoc())
			$id = $row['id'];
			$token_delete = $row['token'];
			$type = $row['type'];
			$file_name = $row['name'];
			$ext = $row['ext'];
			$date_now = date('Y-m-d H:i:s');
			$expire_date = $row['date_expire'];

			$diff = strtotime($expire_date) - strtotime($date_now);

			if ($diff<=0)
				$dir = glob($path.'/uploads/*');
				if ($type == 'file')
					foreach ($dir as $file)
						$file = str_replace($path."/uploads/", "",$file);			
						$token_hashed = hash('tiger192,3',$file);			
						if ($token_hashed==$token_delete)
							$clean = "DELETE FROM uploads WHERE id='$id'";		
							array_map('unlink', glob($path."/uploads/$file/$id$ext"));
							if (is_dir_empty($path."/uploads/$file")) {
	function is_dir_empty($dir) {
	  if (!is_readable($dir)) return NULL; 
	  return (count(scandir($dir)) == 2);
There must be some difference preventing the file from being deleted, do you have an exact copy of the files on your localhost where it works 100% compared to your server? That may help you to work it out. Something else that may help is using transactions on your script, this will help you figure out which files are giving you the problems.

Another alternative is to re-write the script so that you can use the database to do all of the work using the where clause, you currently select everything and loop through it, SQL can delete all of your data entries for you. Then step through the file structure to see what needs deleting?
Probably yes i will rewrite code

And yes script is 100% same on localhost and server, at first i thought file names is a problem "my file.png" "my file's.png" so what i did was i gave file random name like "43rserwe.png" saved original name on DB, so now when server needs to delete file it deletes "43rserwe.png" and not "my file.png" thought that would fix the problem but still no it's weird because some files get delete and some not, there is no specific type of file that doesn't get deleted it can be any file, my thought was that when array_map is executed it starts to delete file and then jumps to other row (if deletion takes too much time it simply doesn't delete it) will rewrite script, just thought will get answer here why is this happening.

The code you provided deletes expired files from a directory on your server. If the file deletion doesn't work consistently on the server, here are a few possible causes:

  1. File permissions: Check that the PHP script has the necessary permissions to delete files in the directory.
  2. Directory traversal: Confirm that the generated file paths from glob() match the actual paths of the files on the server.
  3. Timing issues: Account for any slight time discrepancies between the server and the expiration dates stored in the database.
  4. Database synchronization: Ensure the expiration dates in the server's uploads table are synchronized with the localhost version.
To debug the issue, add debug output to track variables and paths. Verify file permissions, check database synchronization, and adjust the code accordingly.