<?php
class filehandling
{
public static function folderHasToExist($folder, $mask=0775)
{
if (!is_dir($folder)) {
mkdir($folder, $mask, true);
}
}
public static function getDirectories($path, &$directories) {
if (is_dir($path))
if ($dh = opendir($path)) {
$i=0;
while (($dir = readdir($dh)) !== false) {
if (filetype($path . $dir) == "dir" && $dir != "." && $dir != "..") {
$directories[$i] = $dir;
$i++;
}
}
closedir($dh);
return true;
}
return false;
}
public static function getExtension($filename)
{
return strtolower(substr(strrchr($filename, '.'), 1));
}
public static function getFiles($path, &$files) {
if (is_dir($path))
if ($dh = opendir($path)) {
$i=0;
while (($file = readdir($dh)) !== false) {
if (filetype($path . $file) == "file" && filehandling::checkExt($file)) {
$files[$i] = $file;
$i++;
}
}
closedir($dh);
return true;
}
return false;
}
public static function scanDirectory($dir) {
$FileHandling = new classFileHandling;
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (filetype($dir . $file) == "dir" && $file != "." && $file != "..") {
$dirs[] = $dir . $file . '/';
$sub_dirs = $this->ScanDirectory($dir . $file . '/');
if (!empty($sub_dirs))
foreach ($sub_dirs as $sub_dir)
$dirs[] = $sub_dir;
}
}
closedir($dh);
}
return $dirs;
}
public static function getParentDir($dir) { // GetParentDir('/a/b/c/') = '/a/b/'
if (!strpos(rtrim($dir, '/'), '/')) return "";
$path_string = substr($dir, 0, -(strlen(ltrim(strrchr(rtrim($dir, '/'), '/'), '/'))+1));
if (empty($path_string))
return '/';
else
return $path_string;
}
public static function getUnusedFileName($filename)
{
if (is_file($filename)) {
$new_filename = $filename;
for ($i=2; is_file($new_filename); $i++) {
$ext = self::getExtension($filename);
if (!empty($ext)) {
$new_filename = substr($filename, 0, strlen($filename)-strlen($ext)-1) . '-' . $i . '.' . $ext;
} else {
$new_filename = $filename . '-' . $i;
}
}
$filename = $new_filename;
}
return $filename;
}
public static function stripWeirdCharacters($filename)
{
return preg_replace('/[^A-Za-z0-9\.\/\-\_\+\%]/', '',
preg_replace('/ /', '-', $filename));
}
public static function unzip($src_file, $dest_dir, $createSubfolders=true, $overwrite=false)
{
ini_set('memory_limit','128M'); // Mem. req
if(!is_resource(zip_open($src_file)))
{
$src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
}
if (is_resource($zip = zip_open($src_file)))
{
// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Open the entry
if (zip_entry_open($zip,$zip_entry,"r"))
{
// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);
if (!$overwrite)
$file_name = self::getUnusedFileName($file_name);
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
if (!file_exists($file_name) && $file_name[strlen($file_name)-1]=='/' && $createSubfolders)
mkdir($file_name);
if(!is_dir($file_name))
file_put_contents($file_name, $fstream );
// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
}
else
{
//echo "No Zip Archive Found.";
return false;
}
return true;
}
public static public function checkExt($filename)
{
return (strtolower(substr($filename, -4)) == '.jpg');
}
}