I have an error and it is that I have a folder in which I upload some documents, and when new ones are uploaded, it must delete the ones that were already in the folder, the problem is that it does not let me, the following error appears:
Warning : unlink(C:/xampp/htdocs/Jomar/induccion/documents/politicas/POLITICA JOMAR INVERSIONES SAS 2019.pdf): Resource temporarily unavailable in C:\xampp\htdocs\Jomar\induccion\controllers\FilesController.php on line twenty-one
The code in which the error occurs is as follows:
public static function deleteFiles($ruta){
$files = glob("$ruta*.pdf"); //obtenemos todos los nombres de los ficheros
if($files != null){
foreach($files as $file){
if(is_file($file))
$res = unlink($file); //elimino el fichero
}
}else{
$res = true;
}
return $res;
}
And this is where I call that function:
$dir = "/Jomar/induccion/documents/";
if(isset($_FILES["politicas"]) && $_FILES["politicas"] != null){
$fichero = $_SERVER['DOCUMENT_ROOT']."{$dir}politicas/";
if(FilesController::deleteFiles($fichero)){
for($i=0; $i < count($_FILES["politicas"]["tmp_name"]); $i++){
if(!move_uploaded_file($_FILES["politicas"]["tmp_name"][$i], $fichero.basename($_FILES["politicas"]["name"][$i]))){
$cont++;
}
}
}
}
If someone can help me, I appreciate it.
The problem is that the file is being used by another process, or perhaps itself.
Try changing the
if(is_file($file))
toif(is_writable($file))
With that you check if you can write that file, because if you can write it you can delete it.