Source for file Files.php
Documentation is available at Files.php
* @author $Author: Wei Zhuo $
* @version $Id: Files.php 26 2004-03-31 02:35:21Z Wei Zhuo $
define('FILE_ERROR_NO_SOURCE', 100);
define('FILE_ERROR_COPY_FAILED', 101);
define('FILE_ERROR_DST_DIR_FAILED', 102);
* @author $Author: Wei Zhuo $
* @version $Id: Files.php 26 2004-03-31 02:35:21Z Wei Zhuo $
* Copy a file from source to destination. If unique == true, then if
* the destination exists, it will be renamed by appending an increamenting
* @param string $source where the file is from, full path to the files required
* @param string $destination_file name of the new file, just the filename
* @param string $destination_dir where the files, just the destination dir,
* e.g., /www/html/gallery/
* @param boolean $unique create unique destination file if true.
* @return string the new copied filename, else error if anything goes bad.
function copyFile($source, $destination_dir, $destination_file, $unique= true)
$destination_file= strtr($destination_file,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn");
$dotIndex = strrpos($destination_file, '.');
$ext = substr($destination_file, $dotIndex);
$base = substr($destination_file, 0, $dotIndex);
while(is_file($destination_dir. $filename))
$filename = $base. '_'. $counter. $ext;
if (!copy($source, $destination_dir. $filename))
//verify that it copied, new file must exists
if (is_file($destination_dir. $filename))
* @param string $newFolder specifiy the full path of the new folder.
* @return boolean true if the new folder is created, false otherwise.
$perm = octdec(!empty($perm)? $perm: '0770');
mkdir ($newFolder, $perm);
return chmod($newFolder, $perm);
* Escape the filenames, any non-word characters will be
* replaced by an underscore.
* @param string $filename the orginal filename
* @return string the escaped safe filename
* @param string $file file to be deleted
* @return boolean true if deleted, false otherwise.
* Delete folder(s), can delete recursively.
* @param string $folder the folder to be deleted.
* @param boolean $recursive if true, all files and sub-directories
* are delete. If false, tries to delete the folder, can throw
* error if the directory is not empty.
* @return boolean true if deleted.
function delFolder($folder, $recursive= false)
while (false !== ($entry = $d->read()))
if ($entry != '.' && $entry != '..')
//$folder= $folder.'/thumbs';
$deleted &= rmdir($folder);
* Append a / to the path if required.
* @param string $path the path
* @return string path with trailing /
//append a slash to the path if it doesn't exists.
if(!(substr($path,- 1) == '/'))
* Concat two paths together. Basically $pathA+$pathB
* @param string $pathA path one
* @param string $pathB path two
* @return string a trailing slash combinded path.
* Similar to makePath, but the second parameter
* is not only a path, it may contain say a file ending.
* @param string $pathA the leading path
* @param string $pathB the ending path with file
* @return string combined file path.
* Format the file size, limits to Mb.
* @param int $size the raw filesize
* @return string formated file size.
else if($size >= 1024 && $size < 1024* 1024)
return sprintf('%01.2f',$size/ 1024.0). ' Kb';
return sprintf('%01.2f',$size/ (1024.0* 1024)). ' Mb';
|