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.
我正在为我的网站资产进行图像压缩cron作业。我面临的问题是代码在本地工作正常但在远程服务器上没有。
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.
我正在使用scandir,我已经看到了相关帖子:php scandir()没有显示文件 - 只显示目录用户说它不是递归的。但是在我的本地系统上,我已经复制了远程服务器上的文件夹结构,它运行正常。
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.
当我在文件夹上使用var_dump时,我得到了正确的结果。它列出了指定目录中的所有文件夹。
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
.
因此var_dump($ folders)显示正确的目录。当我做var_dump($ files)时,我得到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;
}
?>
1 个解决方案
#1
scandir
only returns the filenames without path. You need to append the path of the original folder to the new one's.
scandir只返回没有路径的文件名。您需要将原始文件夹的路径附加到新文件夹的路径。
$path = "site/assets/files"
$folders = getFilesInDir($path);
foreach($folders as $folder)
{
$files = getFilesInDir($path . "/" . $folder);
var_dump($files);
Hope this does it.
希望这样做。
#1
scandir
only returns the filenames without path. You need to append the path of the original folder to the new one's.
scandir只返回没有路径的文件名。您需要将原始文件夹的路径附加到新文件夹的路径。
$path = "site/assets/files"
$folders = getFilesInDir($path);
foreach($folders as $folder)
{
$files = getFilesInDir($path . "/" . $folder);
var_dump($files);
Hope this does it.
希望这样做。