public static function randFilePathFromDir($dir, $type = 'image') { pkdebug("DIR: [{$dir}] "); if (!is_dir($dir)) { pkdebug("Couldn't resolve [{$dir}] to a directory"); return null; } $tmpdir = static::$tmpdir; if (!is_dir($tmpdir)) { mkdir($tmpdir); } $entries = scandir($dir); pkdebug("Entries: ", $entries); if (!is_array($entries) || !sizeOf($entries)) { pkdebug("No entries in [{$dir}]. Entries:", $entries); return null; } #Make array of valid file type paths from entries $paths = []; //$validentries = []; foreach ($entries as $entry) { $newpath = pkuntrailingslashit($dir) . "/{$entry}"; if (!is_file($newpath)) { continue; } // $validentries[]=$entry; // $valid = BaseFileHandler::validateFiletype($newpath, $type); // if ($valid === false) continue; $paths[] = $newpath; } pkdebug("For [{$dir}], got valid paths:", $paths); #We have a set of valid file paths of the required type. Pick one: $path = static::randData($paths); $base = basename($path); $copypath = pkuntrailingslashit(static::$tmpdir) . "/{$base}"; copy($path, $copypath); //Copy it to return $copypath; /* // $relPath = BaseFileHandler::relPathFromFullPath($path); //pkdebug("Your rel path:", $relPath); $mimeType = getFileMimeType($newpath); $newFileObjArgs = [ 'mimetype' => $mimeType, 'path' => $relPath, 'type' => $type, ]; $fileObj = BaseFileHandler::makeNewFileObj($newFileObjArgs); if (!($fileObj instanceOf BaseFile)) return false; //pkdebug("Wow - made a file obj!"); $fileObj->save(); $fileObjId = $fileObj->getId(); return $fileObjId; #We have copied a file into our upload dir. Now let's make a file obj of it - //} * */ }
/** * Appends a trailing slash. * * Will remove trailing forward and backslashes if it exists already before adding * a trailing forward slash. This prevents double slashing a string or path. * * The primary use of this is for paths and thus should be used for paths. It is * not restricted to paths and offers no specific path support. * * @since 1.2.0 * * @param string $string What to add the trailing slash to. * @return string String with trailing slash added. */ function pktrailingslashit($string) { return pkuntrailingslashit($string) . '/'; }