Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('BASEPATH'))
  4.     exit('No direct script access allowed');
  5.  
  6. /*
  7.  * InvoicePlane
  8.  *
  9.  * A free and open source web based invoicing system
  10.  *
  11.  * @package     InvoicePlane
  12.  * @author      Kovah (www.kovah.de)
  13.  * @copyright   Copyright (c) 2012 - 2015 InvoicePlane.com
  14.  * @license     https://invoiceplane.com/license.txt
  15.  * @link        https://invoiceplane.com
  16.  *
  17.  */
  18.  
  19. class Upload extends Admin_Controller
  20. {
  21.     public $targetPath;
  22.  
  23.     public function __construct()
  24.     {
  25.         parent::__construct();
  26.         $this->load->model('upload/mdl_uploads');
  27.         $this->targetPath = getcwd() . '/uploads/customer_files';
  28.  
  29.     }
  30.  
  31.     public function upload_file($customerId, $url_key)
  32.     {
  33.         ini_set('display_startup_errors',1);
  34.         ini_set('display_errors',1);
  35.         error_reporting(-1);
  36.        
  37.         Upload::create_dir($this->targetPath . '/');
  38.  
  39.         if (!empty($_FILES)) {
  40.             $tempFile = $_FILES['file']['tmp_name'];
  41.             $fileName = preg_replace('/\s+/', '_', $_FILES['file']['name']);
  42.             $targetFile = $this->targetPath . '/' . $url_key . '_' . $fileName;
  43.             $file_exists = file_exists($targetFile);
  44.  
  45.             if (!$file_exists) //If file does not exists then upload
  46.             {
  47.                 $data = ['client_id' => $customerId, 'url_key' => $url_key, 'file_name_original' => $fileName, 'file_name_new' => $url_key . '_' . $fileName];
  48.                 $this->mdl_uploads->create($data);
  49.  
  50.                 move_uploaded_file($tempFile, $targetFile);
  51.             } else //If file exists then echo the error and set a http error response
  52.             {
  53.                 echo lang('error_dublicate_file');;
  54.                 http_response_code(404);
  55.             }
  56.  
  57.         } else {
  58.             return Upload::show_files($url_key, $customerId);
  59.         }
  60.     }
  61.  
  62.     // public function file_delete($customerId,$id,$fileName)
  63.     public function delete_file($url_key)
  64.     {
  65.         $path = $this->targetPath;
  66.         $fileName = $_POST['name'];
  67.  
  68.         $this->mdl_uploads->delete($url_key, $fileName);
  69.         unlink($path . '/' . $url_key . '_' . $fileName);
  70.  
  71.     }
  72.  
  73.     public function create_dir($path, $chmod = '0777')
  74.     {
  75.         if (!(is_dir($path) OR is_link($path)))
  76.             return mkdir($path, $chmod);
  77.         else
  78.             return false;
  79.     }
  80.  
  81.     public function show_files($url_key, $customerId = NULL)
  82.     {
  83.  
  84.         $result = array();
  85.         $path = $this->targetPath;
  86.  
  87.         $files = scandir($path);
  88.         if ($files !== false) {
  89.             foreach ($files as $file) {
  90.                 if ('.' != $file && '..' != $file && strpos($file, $url_key) !== false) {
  91.                     $obj['name'] = substr($file, strpos($file, '_', 1) + 1);
  92.                     $obj['fullname'] = $file;
  93.                     $obj['size'] = filesize($path . '/' . $file);
  94.                     $obj['fullpath'] = $path . '/' . $file;
  95.                     $result[] = $obj;
  96.                 }
  97.             }
  98.         } else {
  99.  
  100.             return false;
  101.         }
  102.         echo json_encode($result);
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement