Listing all images in a directory using PHP

前端 未结 8 771
悲&欢浪女
悲&欢浪女 2021-01-04 08:39

I have the code below that lists all the images in a folder, the problem is that it finds some files ( a . and a ..) that I am not sure what they are so I a

8条回答
  •  无人及你
    2021-01-04 08:58

    Please use the following code to read images from the folder.

    function readDataFromImageFolder() {
    
    $imageFolderName = 14;
    $base = dirname(__FILE__);
    $dirname = $base.DS.'images'.DS.$imageFolderName.DS;
    $files = array();
    
    if (!file_exists($dirname)) {
        echo "The directory $dirname not exists.".PHP_EOL;
        exit;
    } else {
        echo "The directory $dirname exists.".PHP_EOL;
        $dh  = opendir( $dirname );
    
        while (false !== ($filename = readdir($dh))) {
            if ($filename === '.' || $filename === '..') continue;
            $files[] = $dirname.$filename;
        }
        uploadImages( $files );
    }
    }
    

    Please click here for detailed explanation. http://www.pearlbells.co.uk/code-snippets/read-images-folder-php/

提交回复
热议问题