4 * Support functions for the importImages script
8 * @author Rob Church <robchur@gmail.com>
12 * Search a directory for files with one of a set of extensions
14 * @param $dir Path to directory to search
15 * @param $exts Array of extensions to search for
16 * @return mixed Array of filenames on success, or false on failure
18 function findFiles( $dir, $exts ) {
19 if( is_dir( $dir ) ) {
20 if( $dhl = opendir( $dir ) ) {
22 while( ( $file = readdir( $dhl ) ) !== false ) {
23 if( is_file( $dir . '/' . $file ) ) {
24 list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
25 if( array_search( strtolower( $ext ), $exts ) !== false )
26 $files[] = $dir . '/' . $file;
39 * Split a filename into filename and extension
41 * @param $filename Filename
44 function splitFilename( $filename ) {
45 $parts = explode( '.', $filename );
46 $ext = $parts[ count( $parts ) - 1 ];
47 unset( $parts[ count( $parts ) - 1 ] );
48 $fname = implode( '.', $parts );
49 return array( $fname, $ext );
53 * Find an auxilliary file with the given extension, matching
54 * the give base file path. $maxStrip determines how many extensions
55 * may be stripped from the original file name before appending the
56 * new extension. For example, with $maxStrip = 1 (the default),
57 * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
58 * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
59 * acme.txt would also be acceptable.
61 * @param $file base path
62 * @param $auxExtension the extension to be appended to the base path
63 * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
64 * @return string or false
66 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
67 if ( strpos( $auxExtension, '.' ) !== 0 ) {
68 $auxExtension = '.' . $auxExtension;
71 $d = dirname( $file );
72 $n = basename( $file );
74 while ( $maxStrip >= 0 ) {
75 $f = $d . '/' . $n . $auxExtension;
77 if ( file_exists( $f ) ) {
81 $idx = strrpos( $n, '.' );
84 $n = substr( $n, 0, $idx );