Source for file filemanager.class.php
Documentation is available at filemanager.class.php
* PHPDevShell is a RAD Framework aimed at developing administrative applications.
* @link http://www.phpdevshell.org
* @copyright Copyright (C) 2007 Jason Schoeman, All rights reserved.
* @license GNU/LGPL, see readme/licensed_under_lgpl or http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* @author Jason Schoeman, Contact: titan [at] phpdevshell [dot] org.
* Copyright notice: See readme/notice
* By using PHPDevShell you agree to notice and license, if you dont agree to this notice/license you are not allowed to use PHPDevShell.
* This class handles file uploads in its simplest form.
* Should file uploads be logged to the database.
* Default will load settings from database.
* The default upload relative path for files.
* Default will load settings from database.
* Set permission for newly uploaded files.
* Default will load settings from database.
public $cmod =
'default';
* The maximum allowed file upload size.
* Default will load settings from database.
* The maximum allowed image upload size.
* Default will load settings from database.
* The allowed extentions for uploads.
* Default will load settings from database.
* Should thumbnails be created on image uploads.
* Default will load settings from database.
* Image quality of converted images.
* Default will load settings from database.
* Typical resize type for thumbnails.
* Default will load settings from database.
* Options : resize | resizepercent | cropfromcenter | crop
* Default will load settings from database.
* [Max Width, Max Height] example (resize image to no wider than 250 pixels wide and 250 pixels high) : 250,250
* Default will load settings from database.
* [Percentage] example (reduce the image by 50%) : 50
* Default will load settings from database.
* [Crop Size] example (create a 100x100 pixel crop from the center of an image) : 100
* Default will load settings from database.
* [startX, startY, width, height] example (create a 100x50 pixel crop from the top left corner of an image) : 0,0,100,50
* Default will load settings from database.
* Default will load settings from database.
* Data fields expected are [[Percentage of image], [Reflection percentage], [Transparency of reflection], [Set Border], [Border Color]. Example : 40,40,80,true,#a4a4a4
* This option will shrink a large image to a smaller then original viewable image. This should be larger then a thumbnail in most cases as this is the image the user can see when clicking on a thumbnail.
* Default will load settings from database.
* [Max Width, Max Height] example (resize image to no wider than 500 pixels wide and 500 pixels high) : 500,500
* Default will load settings from database.
* An alias to group the images with in a certain application.
* Will use active plugin name if left empty.
* Groups file upload by menu id.
* Will use active menu id if left empty.
* Groups file uploads to a specific document.
* Will use active menu id if left empty.
* The group a file batch belongs to.
* Will use uploaders primary group id if left empty.
* Convert pdf to image using convert in Linux.
* Please note imagemagick needs to be installed on Linux server.
* Choose the density image will be converted to from pdf.
* When converting to pdf with multiple pages, image copies will be stored here.
* Holds a record of recently uploaded files.
* This method simply renames the file to unix standards.
* @param string $filename
* @param string $replace Replace odd characters with what?
return $core->safe_name($filename, $replace);
* @param string $filename
* Get filename without extension.
* @param string $filename
$filename_ =
explode('.', $filename);
* Takes a filename and checks what icon needs to go with it.
* @param string $extension_lookup
public function icon_type ($extension_lookup)
if (empty($extension_lookup)) return false;
switch ($extension_lookup) {
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/doc.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/xls.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/eps.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/exe.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/fla.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/gif.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/html.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/html.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/jpg.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/jpg.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/pdf.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/psd.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/sig.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/swf.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/video2.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/video2.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/video.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/bin.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/txt.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
$icon =
"<img src=\"templates/{$configuration['template_folder']}/images/icons/unknown.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
* @param string $filename
if (! empty($filename)) {
* Creates directory according to month and year.
* @param string $directory Writable directory where new directories will be created and files stored.
// Create directory strings.
$year_directory =
$directory .
'/' .
$year .
'/';
$full_directory =
$directory .
'/' .
$year .
'/' .
$month .
'/';
$thumb_directory =
$directory .
'/' .
$year .
'/' .
$month .
'/thumbs/';
$resize_directory =
$directory .
'/' .
$year .
'/' .
$month .
'/resize/';
chmod($year_directory, $cmod);
touch($year_directory .
'index.html');
// Check if full folder exists.
chmod($full_directory, $cmod);
touch($full_directory .
'index.html');
// Check if thumbs folder exists.
chmod($thumb_directory, $cmod);
touch($thumb_directory .
'index.html');
// Check if thumbs folder exists.
mkdir($resize_directory);
chmod($resize_directory, $cmod);
touch($resize_directory .
'index.html');
* Move file from temp directory to specified directory.
* @param string $uploaded_filename
* @param string $directory
* @param string $new_filename
public function upload_file ($uploaded_filename, $directory, $new_filename)
* Creates unique and safe filename.
if ($extension ==
false) {
$extension_ =
'.' .
$extension;
$random =
rand(0, 999999999);
* Simplifies the whole process of uploading files with type=file.
* @param string $input_name The name of the <form type="file" name="???" />
* @param string $file_overwrite_id The id of the log that needs to be overwritten.
* @param string $file_description a Simple file description for uploaded item.
* @return string Will return the complete uploaded directory with its filename.
public function auto_upload ($input_name, $file_overwrite_id =
false, $file_description =
false)
global $template, $lang, $db, $configuration, $core;
// Start Exception handling.
// Get database settings.
if (empty($this->setting)) {
$this->setting =
$db->get_settings(array('log_uploads' , 'default_upload_directory' , 'cmod' , 'max_filesize' , 'max_imagesize' , 'allowed_ext' , 'do_create_thumb' , 'do_create_resize_image' , 'image_quality' , 'thumbnail_type' , 'resize_thumb_dimension' , 'resize_image_dimension' , 'resize_thumb_percent' , 'crop_thumb_fromcenter' , 'crop_thumb_dimension' , 'do_thumb_reflect' , 'thumb_reflect_settings', 'graphics_engine'), 'PHPDevShell');
// Do we have files uploaded?
if (! empty($_FILES[$input_name]['tmp_name']) &&
! empty($_FILES[$input_name]['name'])) {
$tmp_name =
$_FILES[$input_name]['tmp_name'];
$original_name =
$_FILES[$input_name]['name'];
$uploaded_size =
$_FILES[$input_name]['size'];
$mime_type =
$_FILES[$input_name]['type'];
// Determine standard file association variables.
(empty($this->sub_id)) ?
$sub_id =
$configuration['m'] :
$sub_id =
$this->sub_id;
(empty($this->menu_id)) ?
$menu_id =
$configuration['m'] :
$menu_id =
$this->menu_id;
(empty($this->alias)) ?
$alias =
$core->active_plugin() :
$alias =
$this->alias;
(empty($file_description)) ?
$file_description =
$original_name :
false;
(empty($this->group_id)) ?
$group_id =
$configuration['user_group'] :
$group_id =
$this->group_id;
// Determine correct file upload options.
$graphics_engine =
$this->setting($this->graphics_engine, $this->setting['graphics_engine'], 'gd');
define('DEFAULT_THUMBLIB_IMPLEMENTATION', $graphics_engine);
$log_uploads =
$this->setting($this->log_uploads, $this->setting['log_uploads'], true, true);
// default_upload_directory
$default_upload_directory =
$this->setting($this->default_upload_directory, $this->setting['default_upload_directory'], 'upload');
$cmod =
$this->setting($this->cmod, octdec($this->setting['cmod']), 0644);
$max_filesize =
$this->setting($this->max_filesize, $this->setting['max_filesize'], false);
$max_imagesize =
$this->setting($this->max_imagesize, $this->setting['max_imagesize'], false);
$allowed_ext =
$this->setting($this->allowed_ext, $this->setting['allowed_ext'], false);
$do_create_thumb =
$this->setting($this->do_create_thumb, $this->setting['do_create_thumb'], true, true);
// do_create_resize_image
$do_create_resize_image =
$this->setting($this->do_create_resize_image, $this->setting['do_create_resize_image'], true, true);
// resize_image_dimension
$resize_image_dimension =
$this->setting($this->resize_image_dimension, $this->setting['resize_image_dimension'], '640,480');
$resize_image_dimension_ =
explode(',', $resize_image_dimension);
$image_quality =
$this->setting($this->image_quality, $this->setting['image_quality'], 80);
if (stripos($mime_type, 'image') !==
false) {
$check_size =
$max_imagesize;
$check_size =
$max_filesize;
// Setup thumbnail settings.
$set_thumb =
array('jpegQuality' =>
$image_quality);
//////////////////////////////////////////////////////////////
if (! empty($check_size) &&
($uploaded_size >
$check_size)) {
// Check extentions allowed.
if (! empty($allowed_ext)) {
// Check if we have the required extention.
$extention_find =
stripos($allowed_ext, $extention_type);
// Check if extention is allowed.
if ($extention_find ===
false) {
throw
new error(sprintf($lang['extention_not_allowed'], $extention_type, $allowed_ext));
//////////////////////////////////////////////////////////////
// Create date directory.
// Create unique filename.
// Move files to correct folder.
if ($this->upload_file($tmp_name, $final_upload_path, $unique_filename)) {
// Check if we have a pdf and if we need to convert it.
if ($this->convert_pdf ==
true &&
$extention_type ==
'pdf') {
$pdf_path =
$final_upload_path .
$unique_filename;
$jpg_file =
str_replace(".pdf", ".jpg", "$unique_filename");
$jpg_path =
$final_upload_path .
$jpg_file;
exec("convert -density {$this->convert_density} -resize
$resize_image_dimension_[0
] -quality
$image_quality $pdf_path $jpg_path");
$mime_type = 'image/jpg';
// Check if system renamed it.
if (file_exists($jpg_path)) {
$unique_filename = $jpg_file;
$unique_filename = str_replace(".jpg", "-0.jpg", "$jpg_file");
// Finally delete old pdf.
if ($mime_upload == 'image') {
$thumbnail_path = $final_upload_path . 'thumbs/' . $unique_filename;
$thumbnail = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
if (! empty($do_create_thumb)) {
$thumbnail_path = $final_upload_path . 'thumbs/' . $unique_filename;
$resizeimg = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
$thumbnail_type = $this->setting($this->thumbnail_type, $this->setting['thumbnail_type'], 'resize');
// Set preferred settings.
switch ($thumbnail_type) {
// resize_thumb_dimension
$resize_thumb_dimension = $this->setting($this->resize_thumb_dimension, $this->setting['resize_thumb_dimension'], '150,150');
$resize_thumb_dimension_ =
explode(',', $resize_thumb_dimension);
$thumbnail->resize($resize_thumb_dimension_[0], $resize_thumb_dimension_[1]);
$resize_thumb_percent =
$this->setting($this->resize_thumb_percent, $this->setting['resize_thumb_percent'], '40');
$thumbnail->resizePercent($resize_thumb_percent);
$crop_thumb_fromcenter =
$this->setting($this->crop_thumb_fromcenter, $this->setting['crop_thumb_fromcenter'], '150');
$thumbnail->cropFromCenter($crop_thumb_fromcenter);
$crop_thumb_dimension =
$this->setting($this->crop_thumb_dimension, $this->setting['crop_thumb_dimension'], '0,0,100,50');
$crop_thumb_dimension_ =
explode(',', $crop_thumb_dimension);
$thumbnail->crop($crop_thumb_dimension_[0], $crop_thumb_dimension_[1], $crop_thumb_dimension_[2], $crop_thumb_dimension_[3]);
// resize_thumb_dimension
$resize_thumb_dimension =
$this->setting($this->resize_thumb_dimension, $this->setting['resize_thumb_dimension'], '150,150');
$resize_thumb_dimension_ =
explode(',', $resize_thumb_dimension);
$thumbnail->resize($resize_thumb_dimension_[0], $resize_thumb_dimension_[1]);
$do_thumb_reflect = $this->setting($this->do_thumb_reflect, $this->setting['do_thumb_reflect'], false, true);
// thumb_reflect_settings
if (! empty($do_thumb_reflect)) {
// thumb_reflect_settings
$thumb_reflect_settings = $this->setting($this->thumb_reflect_settings, $this->setting['thumb_reflect_settings'], '40,40,80,false,#fff');
$thumb_reflect_settings_ =
explode(',', $thumb_reflect_settings);
$thumbnail->createReflection($thumb_reflect_settings_[0], $thumb_reflect_settings_[1], $thumb_reflect_settings_[2], $thumb_reflect_settings_[3], $thumb_reflect_settings_[4]);
//////////////////////////////////////////////
// Continue saving thumb image well. /////////
//////////////////////////////////////////////
// Finaly save thumbnail!
$thumbnail->save($thumbnail_path);
// Set needed permissions.
chmod($thumbnail_path, $cmod);
//////////////////////////////////////////////
// Continue saving a resized image as well. //
//////////////////////////////////////////////
if (! empty($do_create_resize_image)) {
$resized_view_path = $final_upload_path . 'resize/' . $unique_filename;
$resizeimg = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
// Resize view image if we need it.
$dimension = $resizeimg->getCurrentDimensions();
if ($dimension['width'] >
$resize_image_dimension_[0] ||
$dimension['height'] >
$resize_image_dimension_[1]) $resizeimg->resize($resize_image_dimension_[0], $resize_image_dimension_[1]);
$resizeimg->save($resized_view_path);
// Set needed permissions.
chmod($resized_view_path, $cmod);
// We dont have an image so we wont have thumbs and resized images.
$resized_view_path = false;
if (! empty($log_uploads)) {
// Now that all is good, lets log this entry to the database.
_db_core_upload_logs (file_id, sub_id, menu_id, alias, original_filename, new_filename, relative_path, thumbnail, resized, extention, mime_type, file_desc, group_id, user_id, date_stored, file_size)
('$file_overwrite_id', '$sub_id', '$menu_id', '$alias', '$original_name', '$unique_filename', '$final_upload_path', '$thumbnail_path', '$resized_view_path', '$extention_type', '$mime_type', '$file_description', '$group_id', '{
$configuration['user_id']}', '{
$configuration['time']}', '
$uploaded_size')
// Set needed permissions.
chmod($final_upload_path . $unique_filename, $cmod);
// Return filename and relative path.
return $final_upload_path . $unique_filename;
throw new error($e->getMessage());
* Convert numeric into file size.
* @param numeric $filesize
* @author info@levaravel.com
public function display_filesize ($filesize)
if (is_numeric($filesize)) {
$prefix = array($lang['bytes'] , $lang['kb'] , $lang['mb'] , $lang['gb'] , $lang['tb'] , $lang['pb']);
while (($filesize / $decr) > 0.9) {
$filesize = $filesize / $decr;
return round($filesize, 2) . ' ' . $prefix[$step];
* Internal check to see what thumbnail what settings to return.
* @param mixed $preferred_setting First passed through settings.
* @param mixed $database_setting Database called settings.
* @param mixed $fail_setting If all else fail use this setting.
* @param boolean $boolean Is setting a boolean value?
private function setting ($preferred_setting, $database_setting, $fail_setting, $boolean = false)
if ($preferred_setting == 'default' && ! empty($database_setting)) {
return $database_setting;
} else if (! empty($preferred_setting)) {
return $preferred_setting;
if ($preferred_setting == 'default' && isset($database_setting)) {
return $database_setting;
} else if (isset($preferred_setting)) {
return $preferred_setting;
* Build query for extracting stored files.
* @param integer $file_id Get only a single file with a file_id.
* @param string $alias Load only files by this alias.
* @param integer $sub_id Load only files by this sub_id.
* @param integer $menu_id Load only files by this menu_id.
private function build_file_query ($file_id = false, $alias = false, $sub_id = false, $menu_id = false)
// Check if we need a single file.
$query_grouped = " WHERE file_id = '
$file_id'
";
// Group WHERE queries first level.
$query_grouped .= " WHERE alias = '
$alias'
";
} else if (! empty($sub_id)) {
$query_grouped .= " WHERE sub_id = '
$sub_id'
";
} else if (! empty($menu_id)) {
$query_grouped .= " WHERE menu_id = '
$menu_id'
";
// Group AND queries second level.
if (! empty($query_grouped)) {
if (! empty($sub_id) && empty($sub_id_set)) {
$query_grouped .= " AND sub_id = '
$sub_id'
";
if (! empty($menu_id) && empty($menu_id_set)) {
$query_grouped .= " AND menu_id = '
$menu_id'
";
// We need no queries here.
* Delete file logs from database.
* @param integer $file_id Get only a single file with a file_id.
* @param string $alias Load only files by this alias.
* @param integer $sub_id Load only files by this sub_id.
* @param integer $menu_id Load only files by this menu_id.
* @return boolean Will return true if deleted.
public function delete_files ($file_id = false, $alias = false, $sub_id = false, $menu_id = false)
$files = $this->load_files($file_id, $alias, $sub_id, $menu_id);
$query_grouped = $this->build_file_query($file_id, $alias, $sub_id, $menu_id);
foreach ($files as $fa) {
// Check if multiple images exists of a pdf conversion left over.
if (strstr($fa['download_file'], '-0.jpg')) {
// Loop multiple images and list them.
if (! empty($fa['download_file'])) $this->delete_filename($fa['download_file']);
* Load uploaded files from the log database.
* @param integer $file_id Get only a single file with a file_id.
* @param string $alias Load only files by this alias.
* @param integer $sub_id Load only files by this sub_id.
* @param integer $menu_id Load only files by this menu_id.
* @param string $order What the query should be ordered by. allows you to order by any of these column values : file_id, sub_id, menu_id, alias, original_filename, new_filename, relative_path, thumbnail, resized, extention, mime_type, file_desc, group_id, user_id, date_stored
* @param string $limit how many files should be returned in the array.
public function load_files ($file_id = false, $alias = false, $sub_id = false, $menu_id = false, $order = 'file_id DESC', $limit = '0,5')
$query_grouped = $this->build_file_query($file_id, $alias, $sub_id, $menu_id);
// Create order and limit query.
$order_by = " ORDER BY
$order LIMIT
$limit";
// Load files from database.
$load_files_db = $db->new_query("
// Loop and gather results.
while ($files_array = mysql_fetch_array($load_files_db)) {
// Create download filename.
$files_array['download_file'] = $files_array['relative_path'] . $files_array['new_filename'];
// Create human readable file size.
$files_array['format_file_size'] = $this->display_filesize($files_array['file_size']);
// Create extention image.
$files_array['extention_img'] = $this->icon_type($files_array['extention']);
$final_files_return[$files_array['file_id']] = $files_array;
if (! empty($final_files_return)) {
return $final_files_return;
* Check if multiple images exists converted from a pdf with multiple pages.
* @param int $nr The current file number in the loop.
// Multiple files should exist, recall and check.
$file_next = str_replace("-
$nr.jpg
", "-
$nr_next.jpg
", "$file");
* Enables the caller to download a file to a local writable directory.
* @param $url The url to download from
* @param $copy_to The location to copy to add the name to the file too, example: /var/www/test.zip
public function download_file($url, $copy_to)
// Check if function exists.
if (!function_exists('curl_init')) {
throw new <a href="../PHPDevShell/error.html">error</a>(__('Oops cURL is not installed on your server, I cannot continue.', $this->d));
$out = fopen($copy_to, 'wb');
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('File %s could not be written to %s, please check permission, directories and if file exists.', $this->d), $url, $copy_to));
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('There was a cURL error : %s when I tried to copy to : %s',$this->d), curl_error($ch), $copy_to));
* Extracts a ZIP archive to the specified extract path
* @param string $file The ZIP archive to extract (including the path)
* @param string $extractPath The path to extract the ZIP archive to
* @return boolean TURE if the ZIP archive is successfully extracted, FALSE if there was an errror
$res = $zip->open($file);
$zip->extractTo($extractPath);
* Copy file or folder from source to destination, it can do
* recursive copy as well and is very smart
* It recursively creates the dest file or directory path if there weren't exists
* - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
* - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
* - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest
* - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
* - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
* - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
* - Should have rollback technique so it can undo the copy when it wasn't successful
* - Auto destination technique should be possible to turn off
* - Supporting callback function
* - May prevent some issues on shared enviroments : http://us3.php.net/umask
* @param $source //file or folder
* @param $dest ///file or folder
* @param $options //folderPermission,filePermission
public function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0644))
if ($dest[strlen($dest)-1]=='/') {
cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
$result=copy($source, $__dest);
chmod($__dest,$options['filePermission']);
if ($dest[strlen($dest)-1]=='/') {
if ($source[strlen($source)-1]=='/') {
//Change parent itself and its contents
chmod($dest,$options['filePermission']);
if ($source[strlen($source)-1]=='/') {
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
//Copy parent directory with new name and all its content
@mkdir($dest,$options['folderPermission']);
chmod($dest,$options['filePermission']);
if($file!="." && $file!="..")
if(!is_dir($source."/".$file)) {
//echo "$source/$file ||| $__dest<br />";
$result = $this->smartCopy($source."/".$file, $__dest, $options);
* Will delete a whole directory recursively (directory muste be writable).
* @param $dirname Directory to delete.
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
* Connects to a normal ftp or secure server.
* If values left empty system will use PHPDevShell configuration values.
* @return resource connection id.
public function establish_ftp ($username='config', $password='config', $host='config', $port='config', $ssl='config', $timeout='config', $chdir='config')
// Load database settings.
$conf = $db->get_settings(array('ftp_enable', 'ftp_username', 'ftp_password', 'ftp_host', 'ftp_port', 'ftp_ssl', 'ftp_timeout', 'ftp_root'));
if (!empty($conf['ftp_enable'])) {
if ($username == 'config') $username = $conf['ftp_username'];
if ($password == 'config') $password = $conf['ftp_password'];
if ($host == 'config') $host = $conf['ftp_host'];
if ($port == 'config') $port = $conf['ftp_port'];
if ($ssl == 'config') $ssl = $conf['ftp_ssl'];
if ($timeout == 'config') $timeout = $conf['ftp_timeout'];
if ($chdir == 'config' && ! empty($conf['ftp_root'])) $chdir = $conf['ftp_root'];
if (!$resource = ftp_connect($host, $port, $timeout)) {
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('I could not connect to local ftp server %s, user %s.', $this->d), $host, $username));
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('I could not connect using SSL to local ftp server %s, user %s.', $this->d), $host, $username));
if (ftp_login($resource, $username, $password)) {
// Should we change the directory path.
if (! empty($chdir) && $chdir != 'config') {
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('Incorrect login for local ftp server %s, user %s.', $this->d), $host, $username));
* The following is a fully tested function (based on a previous note) that recursively puts files from a source directory to a destination directory. See http://rufy.com/tech/archives/000026.html for more information.
* NOTE: Use full path name for the destination directory and the destination directory must already exist
* @param resource $conn_id
public function ftp_rcopy($conn_id, $src_dir, $dst_dir)
if ($dir = dir($src_dir)) {
while($file = $dir->read()) { // do this for each file in the directory
if ($file != '.' && $file != '..') { // to prevent an infinite loop
if (is_dir($src_dir . '/' . $file)) { // do the following if it is a directory
if (!@ftp_nlist($conn_id, $dst_dir . '/' . $file)) {
ftp_mkdir($conn_id, $dst_dir . '/' . $file); // create directories that do not yet exist
ftp_chmod($conn_id, 0755, $dst_dir . '/' . $file);
$this->ftp_rcopy($conn_id, $src_dir . '/' . $file, $dst_dir . '/' . $file); // recursive part
$this->upload_history[] = array('from' => $src_dir . '/' . $file, 'to' => $dst_dir . '/' . $file);
if (!ftp_put($conn_id, $dst_dir . '/' . $file, $src_dir . '/' . $file, FTP_BINARY)) { // put the files
throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('There was a problem copying file %s',$this->d), $dst_dir . '/' . $file));
$this->upload_history[] = array('from' => $src_dir . '/' . $file, 'to' => $dst_dir . '/' . $file);
ftp_chmod($conn_id, 0644, $dst_dir . '/' . $file);
Documentation generated on Sun, 09 Aug 2009 08:06:20 +0200 by phpDocumentor 1.4.1