A
Anonymous
Guest
Hello
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?
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?
Code:
function deleteExpired(){
$path = $_SERVER['DOCUMENT_ROOT'];
include($path.'/includes/connect.php');
$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")) {
rmdir($path."/uploads/$file");
}
mysqli_query($conn,$clean);
}
}
}
}
}
mysqli_close($conn);
}
function is_dir_empty($dir) {
if (!is_readable($dir)) return NULL;
return (count(scandir($dir)) == 2);
}