开发者

How do I see values in an array?

I am trying to create an a list of files within a directory, including those in subdirectories, showing their full path, filename and extension. Any guidance would be appreciated.

After plenty of reading I have attempted several things, this is the most recent I found and tried but have not had any joy, for now I am just looking for the browser to display the above information.

$dir = '../test/images/';
function getFileList($dir, $recurse=false)
{
    // array to hold return value
    $retval = array();
    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";
    // open pointer to directory and read list of files
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(false !== ($entry = $d->read())) {
        // skip hidden files
        if($entry[0] == ".") continue;
        if(is_dir("$dir$entry")) {
            $retval[] = array(
                "name" => "$dir$entry/",
                "type" => filetype("$dir$entry"),
                "size" => 0,
                "lastmod" => filemtime("$dir$entry")
            );
            if($recurse && is_readable("$dir$entry/")) {
                $retval = array_merge($retval, getFileList("$dir$entry/", true));
            }
            } elseif(is_readable("$dir$entry")) {
            $retval[] = array(
                "name" => "$dir$entry",
                "type" => mime_content_type("$dir$entry"),
   开发者_开发百科             "size" => filesize("$dir$entry"),
                "lastmod" => filemtime("$dir$entry")
            );
        }
    }
    $d->close();
    return $retval;
}
print_r($retval);

My ultimate goal is to create some php that on execute will look in a directory, create the subdirectory (and any subdirectories there in) with the same name in a new directory (so /test/album/ would be created as /live/album/). Then resize any images that are contained in each original directory and save them to their related new directory. I appreciate this is a lot and will no doubt have many questions as I proceed, but will save those for later.


Here's a similar script I wrote for my sites:

<?php
$baseDir = '/images/test';
$newDir = '/images/live/';
$returnFiles = array();

function getDirectory($path, $level, $newDir, $baseDir, &$returnFiles) {
    if(substr($path, -1) != DIRECTORY_SEPARATOR) {
        $path = $path . DIRECTORY_SEPARATOR;
    }
    if(substr($newDir, -1) != DIRECTORY_SEPARATOR) {
        $newDir = $newDir . DIRECTORY_SEPARATOR;
    }
    if(substr($baseDir, -1) != DIRECTORY_SEPARATOR) {
        $baseDir = $baseDir . DIRECTORY_SEPARATOR;
    }
    $dh = opendir( $path );
    if(!$dh) {
        return;
    }
    while( false !== ( $file = readdir( $dh ) ) ) {
        if($file == '.' || $file == '..') {
            continue;
        }
        $ident = str_repeat("\t", $level);

        if(is_file($path . $file)) {
            if(substr(mime_content_type($path . $file), 0, 5) == 'image') {
                $returnFiles[] = saveImage($path . $file, $newDir, $baseDir);
            }
        }
        else if(is_dir($path . $file)) {
            getDirectory($path . $file . DIRECTORY_SEPARATOR, $level+1, $newDir, $baseDir, $returnFiles);
        }
    }
    closedir( $dh );
} 

function saveImage($image, $newDir, $baseDir) {
    $current = dirname($image) . DIRECTORY_SEPARATOR;
    $path = str_replace($baseDir, $newDir, $current);
    if(!is_dir($path)) {
        mkdir($path, 0777, TRUE);
    }
    $oldImage = array(
        "name" => $image,
        "type" => mime_content_type($image),
        "size" => filesize($image),
        "lastmod" => filemtime($image)
    );
    // conver image
    convertImage($image, $path);    
    $image = $path . basename($image);
    $newImage = array(
        "name" => $image,
        "type" => mime_content_type($image),
        "size" => filesize($image),
        "lastmod" => filemtime($image)
    );
    return array('old' => $oldImage, 'new' => $newImage);
}

function convertImage($image, $folder) {
    // do what you need to do to convert/resize etc..
    // for demo purpose I simply copy the image.
    copy($image, $folder . basename($image));
}

getDirectory($baseDir, 0, $newDir, $baseDir, $returnFiles);
print_r($returnFiles);


simple and fast :

echo '<pre>';
print_r($array); // where $array == array();
echo '</pre>';


Well to answer your original question, I've found that using var_dump($array) really helps to illustrate the passed arrays structure, especially multidimensional. Pus it gives you the data type which can be helpful as you progress.


Here is some code I picked up at Ubuntu Forums that shows all directories and their sub-directories in a given path.

function recurse($path)
{
    foreach(glob($path.'/*',GLOB_ONLYDIR) as $myDirectory)
    {
        // change this to whatever you need to do
        echo "Directory : $myDirectory<br>\n";

        // but keep this line
        recurse($myDirectory);
    }
}

This should get you started on what you’re trying to do. You might want to change the name of the function for clarity though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜