简体   繁体   English

Scandir不在远程时列出文件

[英]Scandir not listing files when remote

I'm working on an image compression cron job for my sites assets. 我正在为网站资产进行图像压缩Cron工作。 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. 我正在使用scandir ,我已经看过相关的文章: php scandir()不显示文件-仅显示目录,用户在说它不是递归的。 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. 当我在文件夹上使用var_dump时,我得到正确的结果。 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. 因此, var_dump($folders)显示正确的目录。 When I do var_dump($files) I get NULL NULL NULL NULL NULL . 当我执行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;
}

?>

scandir only returns the filenames without path . scandir仅返回不带path的文件名。 You need to append the path of the original folder to the new one's. 您需要将原始文件夹的路径追加到新文件夹的路径。

$path = "site/assets/files"
$folders = getFilesInDir($path);

foreach($folders as $folder)
{
    $files = getFilesInDir($path . "/" . $folder);
    var_dump($files);

Hope this does it. 希望这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM