I'm working on an image compression cron job for my sites assets. The problem I'm facing is that the code works fine locally but not on on the remote server.
I'm using scandir
, I've seen the related post: php scandir() not showing files - only showing directories users were saying that it isn't recursive. However on my local system I've replicated the folder structure on the remote server and it works perfectly.
I have the following function which I use for both folders and files.
function getFilesInDir($path)
{
$directory = $path;
if (is_dir($directory))
{
$files = array();
foreach(scandir($directory) as $file)
{
if ('.' === $file) continue;
if ('..' === $file) continue;
$files[] = $file;
// }
}
}
return $files;
}
When I use var_dump
on the the folder I get the right results. It lists all folders within the specified directory.
Usage
$folders = getFilesInDir("site/assets/files");
foreach($folders as $folder)
{
$files = getFilesInDir($folder);
//...Do the rest
So var_dump($folders)
displays the correct directories. When I do var_dump($files)
I get NULL NULL NULL NULL NULL
.
I reiterate, this works fine on my local machine but not my remote server.
Complete Code (if it's of use) It's not pretty I know but it works and I'm on a deadline.
<?php
// $folders = getFilesInDir(getcwd());
$folders = getFilesInDir("site/assets/files");
foreach($folders as $folder)
{
$files = getFilesInDir($folder);
var_dump($files);
if ($files)
{
$x = array_filter($files, "isImage");
foreach($files as $f)
{
$path_parts = pathinfo($f);
if (@$path_parts['extension'] != null)
{
if (filesize($folder . "/" . $f) > 1000000)
{
echo $f . " - " . filesize($folder . "/" . $f) . "<br />";
if ($path_parts['extension'] == "jpg" || $path_parts['extension'] ==
"jpeg" || $path_parts['extension'] == "png")
{
// Make bin folder if not exists
MakeFolder($folder . "/");
// Compress file in folder to bin folder
$d = compress($folder . "/" . $f, $folder . "/bin/" . $f, 30);
// Delete files in base
unlink($folder . "/" . $f);
// Move files from bin to root
rename($folder . "/bin/" . $f, $folder . "/" . $f);
}
}
}
}
}
}
function MakeFolder($path)
{
if (!file_exists($path . "/bin/"))
{
mkdir($path . "/bin/", 0777, true);
}
}
function isImage($var)
{
$path_parts = pathinfo($var);
if (@$path_parts['extension'])
{
if ($path_parts['extension'] == "jpg" || $path_parts['extension'] == "jpeg" || $path_parts
['extension'] == "png")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
function getFilesInDir($path)
{
$directory = $path;
if (is_dir($directory))
{
$files = array();
foreach(scandir($directory) as $file)
{
if ('.' === $file) continue;
if ('..' === $file) continue;
$files[] = $file;
// }
}
}
return $files;
}
function compress($source, $destination, $quality)
{
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
?>