Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @version $Id: view.html.php 74 2013-02-05 15:00:00Z sbebbers $
- * @package My Pictures v1.0.1
- * @subpackage Components
- * @copyright Copyright (C) 2013 Metapps Ltd
- * @author Shaun Bebbington
- * @link http://www.metaps.co.uk
- * @license License GNU General Public License version 2 or later
- * Use at your own risk - no warranty implied or given
- * @note I wanted a quick and dirty solution to test the logic,
- * so I put the file-handling stuff here. Might not be the
- * best idea ever, but it works for now. Will separate the
- * logic further when I get some time.
- */
- defined('_JEXEC') or die('You do not have the correct permissions to view this page.');
- jimport('joomla.application.component.view');
- class AnotherOneViewAnotherOne extends JView {
- // This will take the image data from the session data:
- public $imgData = array('user' => '',
- 'exts' => '',
- 'fileName' => '',
- );
- // Used for making directories (default is set in default.php):
- public $dir = '';
- public function display( $tpl = null ) {
- // Gets user data:
- $tempUser = array();
- // Get user details into array buffer
- $tempUser = JFactory::getUser();
- // We only need the username:
- $this->imgData['user'] = $tempUser->username;
- // Now get rid of the user details
- $tempUser = null;
- // Example type and name set (for testing purposes):
- $this->imgData['exts'] = 'jpg';
- $this->imgData['fileName'] = 'test';
- JToolBarHelper::title( 'Another One Administration Page' );
- JToolBarHelper::cancel( 'Anotherone.cancel' );
- parent::display( $tpl );
- }
- public function makeDir( $directory = '' ) {
- // Does some checking to see if the session data is stored correctly:
- if( is_null( $_FILES['file']['name'] ) ) {
- echo '<br />No file chosen<br />';
- }
- if( is_null( $directory ) ) {
- echo '<br />No directory name set - please contact the administrator<br />';
- } else {
- // Sets directory name:
- $this->dir = $directory;
- }
- // Checks if the directory already exsists or not:
- if( file_exists( $this->dir ) ) {
- // Sends file data to the upload function:
- $this->upload( $_FILES );
- } else {
- // So, if not (ie, first-time use), let's make it:
- mkdir( $this->dir, 0777 );
- $this->upload( $_FILES );
- }
- }
- public function upload( $file = array() ) {
- // Checks for the correct file types:
- if(! ( ( $file['file']['type']=='image/png' ) ||
- ( $file['file']['type'] == 'image/jpeg' ) ||
- ( $file['file']['type'] == 'image/pjpeg' ) ||
- ( $file['file']['type'] == 'image/gif' ) ) ) {
- echo '<br />Invalid file type';
- }
- if( ( int ) $file['file']['size'] > 8388608 ) {
- // This is just a test: maximum file size is 8 Megabytes
- echo '<br />File too big</br>';
- } else {
- // Error checknig...
- if( ( int ) $file['file']['error'] > 0) {
- echo '<br />Error code: ' . $file['file']['error'] . '<br />';
- } else {
- // Okay, everything seems good, so we may proceed:
- if( isset( $file['file']['name'] ) ) {
- $this->imgData['fileName'] = explode( '.', $file['file']['name'] );
- } else {
- // Maybe everything isn't so good; it shouldn't get to this stage though as the above
- // error checking etc. should be pretty bullet-proof:
- echo '<br />Hmm... this is embarrassing - my code has not picked up the file name.<br />Please try again.';
- }
- // Temp variable:
- $num = count( $this->imgData->exts )-1;
- $this->imgData->exts = $this->exts[$num];
- // Generates a filename with the current user (admin) and a 10-character randomly generated
- // key:
- $this->imgData['fileName'] = $this->imgData['user']
- . $this->generateKey( 6 )
- . '.'
- . $this->imgData['exts'];
- // There isn't much chance of this happening, but if there was the same name already in use then we need to
- // stop as it'll over-write the previous file:
- if( file_exists( $this->dir . '/' . $this->imgData['fileName'] ) ) {
- echo '<br />There seems to be a duplicate file name - which is a rare event<br />'
- . 'due to the randomly generated key. You should buy a lottery ticket.';
- } else {
- $destination = $this->dir . '/' .$this->imgData['fileName'];
- // Okay, so the file doesn't exsist, let's save it:
- move_uploaded_file( $file['file']['tmp_name'], $destination );
- echo '<br />File created: <b>' . $destination . '</b><br />';
- }
- }
- }
- }
- // Generates random key for the file names of the pictures, like Facebook does:
- public function generateKey( $noOfChars = 10 ) {
- $random = 'qwertyuiopasdfghjklzxcvbnm0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- return substr( str_shuffle( $random ),0 ,$noOfChars );
- }
- public function __destruct() {
- // Releases resources:
- $this->imgData = null;
- $this->dir = null;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement