dousilie9522 2015-05-04 15:05
浏览 59
已采纳

Scandir在远程时不列出文件

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;
}

?>
  • 写回答

1条回答 默认 最新

  • doubi1713 2015-05-04 15:15
    关注

    scandir only returns the filenames without 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析