I want to get all images from folder /folder/floder2
and then check whether these Images exists in the DB or not, and then delete if it does not exist in DB
below is my code -
$imgarray= array();
function rsearch() {
global $imgarray;
$directory= $_SERVER['DOCUMENT_ROOT'] . '/folder/folder1';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach($objects as $name){
$imgarray[] = $name;
}
}
$files = rsearch();
foreach ($dbArray as $dbImg ) {
$Img = trim($dbImg['products_image'],"products/");
foreach ($imgarray as $folderImg) {
$ignore = Array(".", "..");
if($Img == $folderImg){
echo 'success';
}else {
unlink($folderImg);
echo 'successfully deleted';
}
}
}
I am getting below type of when print_r($folderImg);
SplFileInfo Object (
[pathName:SplFileInfo:private] => /var/www/vhosts/serverfoldername/project.com/projectfolder/folder/folder1/image1.jpg
[fileName:SplFileInfo:private] => image1.jpg
)
I want to print this to print
[fileName:SplFileInfo:private] => image1.jpg
but i am not getting how to call this type of
Array (
[0] => SplFileInfo Object (
[pathName:SplFileInfo:private] => /var/www/vhosts/serverfoldername/project.com/projectfolder/folder/folder1/image1.jpg
[fileName:SplFileInfo:private] => image1.jpg
)
[1] => SplFileInfo Object (
[pathName:SplFileInfo:private] => /var/www/vhosts/serverfoldername/project.com/projectfolder/folder/folder1/imag2.png
[fileName:SplFileInfo:private] => imag2.png
)
)
This will give the filename of a SplFileInfo Object http://php.net/manual/en/class.splfileinfo.php
print_r($folderImg->getFilename()):