Uploading Files and Images
From PHPDevShell
Thumbnailer Class
PHPDevShell extends the brilliant PHP Thump API, please see this for more information.
The Easy Uploader
PHPDevShell has powerful file and image uploading and managing functionality. This is so often used in applications that it is a essential requirement. The file manager has a storage registry that can be viewed in the System Logs.
View the DummyPlugin upload example for a working model.
Uploaded images can be resized and thumbed automatically. Lets Start...
Make sure your upload folder and settings are set correctly set in the General Settings GUI of PHPDevShell.
To call the filemanager class;
$filemanager = new filemanager();
Good now we have uploading capabilities available in the $filemanager.
Lets setup the file uploader in our php file.
// Heading. $this->template->heading(_('Upload Example.')); $this->template->info(_('Shows how you can upload and list files.')); // Call filemanager. $filemanager = new filemanager(); // Set some parameters, have a look at the API for more. $filemanager->allowed_ext = 'default'; // Will load extentions as set in General Settings GUI $filemanager->alias = 'myfiles'; // Group by this alias $filemanager->sub_id = '1'; // Can be set to an id accommodating a forms database id for instance so one could easily locate it when loading data. $filemanager->convert_pdf = false; // This will convert PDF files to images, smart, I know. // Upload required files. try { // Upload required files. if ($filename = $filemanager->auto_upload('file1')) { $this->template->ok(sprintf(_('File %s was uploaded'), $filename)); } } catch (error $e) { $e->warning(); } // Lets load the files uploaded so far! $uploaded_files_array = $filemanager->load_files(false, 'myfiles', '1', false, 'file_id DESC', '0,3'); // Loop results of uploaded files. foreach ($uploaded_files_array as $files) { $u_arr[] = array( 'download_file' => $files['download_file'], 'thumbnail' => $files['thumbnail'], 'original_filename' => $files['original_filename'] ); } // Assign Variables. $this->template->set('self_url', $this->navigation->self_url()); $this->template->set('u_arr', $u_arr); $this->template->set('absolute_url', $this->configuration['absolute_url'] . '/'); $this->template->show();
We can now build the template, form and a loop to display the 3 files we want to show for this form;
<tr> <td colspan="2"> <form action="{$self_url}" method="post" enctype="multipart/form-data"> <input name="file1" size="45" type="file" /> <input type="submit" value="Upload" name="save" class="button"> </form> </td> </tr> {section name=files loop=$u_arr} {strip} <tr id="highlight" class="{cycle values="alt1,alt2"}"> <th> {$u_arr[files].original_filename} </th> {if $u_arr[files].thumbnail == false} <td> <a href="{$absolute_url}{$u_arr[files].download_file}" target="_blank">{$u_arr[files].original_filename}</a> </td> {else} <td> <a href="{$absolute_url}{$u_arr[files].download_file}" target="_blank"><img src="{$u_arr[files].thumbnail}" alt="{$u_arr[files].original_filename}" title="{$u_arr[files].original_filename}" /></a> </td> {/if} </tr> {/strip} {/section}
As you can see it is rather simple to build a form uploading mechanism. The cool thing about this all is the fact that when it is an images it build a thumbnail, resized and the original image size. These dimension can be controlled for each upload.
Conclusion
You should note that you can have multiple uploading mechanisms and form upload fields, it really supports a vast array of possibilities. You can also upload the files in certain categories for filtering purposes, they include;
/** * Should file uploads be logged to the database. * Default will load settings from database. * * @var boolean */ public $log_uploads = 'default'; /** * The default upload relative path for files. * Default will load settings from database. * Example : myupload * * @var string */ public $default_upload_directory = 'default'; /** * Set permission for newly uploaded files. * Default will load settings from database. * Example : 0777 * * @var int */ public $cmod = 'default'; /** * The maximum allowed file upload size. * Default will load settings from database. * Example : 1000 (1kb) * * @var int */ public $max_filesize = 'default'; /** * The maximum allowed image upload size. * Default will load settings from database. * Example : 1000 (1kb) * * @var int */ public $max_imagesize = 'default'; /** * The allowed extentions for uploads. * Default will load settings from database. * Example : jpg,png,gif * * @var string */ public $allowed_ext = 'default'; /** * Should thumbnails be created on image uploads. * Default will load settings from database. * * @var boolean */ public $do_create_thumb = 'default'; /** * Image quality of converted images. * Default will load settings from database. * Example : 80 * * @var int */ public $image_quality = 'default'; /** * Typical resize type for thumbnails. * Default will load settings from database. * Options : resize | resizepercent | cropfromcenter | crop * * @var string */ public $thumbnail_type = 'default'; /** * Resize by pixels. * 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 * * @var string */ public $resize_thumb_dimension = 'default'; /** * Resize by percentage. * Default will load settings from database. * [Percentage] example (reduce the image by 50%) : 50 * * @var string */ public $resize_thumb_percent = 'default'; /** * Crop from center. * Default will load settings from database. * [Crop Size] example (create a 100x100 pixel crop from the center of an image) : 100 * * @var string */ public $crop_thumb_fromcenter = 'default'; /** * Crop by measure. * 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 * * @var string */ public $crop_thumb_dimension = 'default'; /** * Add thumb reflections. * Default will load settings from database. * * @var boolean */ public $do_thumb_reflect = 'default'; /** * Reflection options. * 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 * * @var string */ public $thumb_reflect_settings = 'default'; /** * 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. * * @var boolean */ public $do_create_resize_image = 'default'; /** * [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. * * @var string */ public $resize_image_dimension = 'default'; /** * An alias to group the images with in a certain application. * Will use active plugin name if left empty. * * @var string */ public $alias; /** * Groups file upload by menu id. * Will use active menu id if left empty. * * @var int */ public $menu_id; /** * Groups file uploads to a specific document. * Will use active menu id if left empty. * * @var int */ public $sub_id; /** * The group a file batch belongs to. * Will use uploaders primary group id if left empty. * * @var int */ public $group_id; /** * Convert pdf to image using convert in Linux. * Please note imagemagick needs to be installed on Linux server. * * @var boolean */ public $convert_pdf = false; /** * Choose the density image will be converted to from pdf. * * @var integer */ public $convert_density = '300'; /** * When converting to pdf with multiple pages, image copies will be stored here. * * @var array */ public $image_copies = array(); /** * This method simply renames the file to unix standards. * * @param string $filename * @param string $replace Replace odd characters with what? */
If these are not changed the defaults as set in the General Settings Gui under File Uploads.

