emprit

upload.php

Jul 4th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.01 KB | None | 0 0
  1. <?php
  2. /*
  3.  * jQuery File Upload Plugin PHP Example 5.2.2
  4.  * https://github.com/blueimp/jQuery-File-Upload
  5.  *
  6.  * Copyright 2010, Sebastian Tschan
  7.  * https://blueimp.net
  8.  *
  9.  * Licensed under the MIT license:
  10.  * http://creativecommons.org/licenses/MIT/
  11.  */
  12. include "koneksi.php";
  13. error_reporting(E_ALL | E_STRICT);
  14.  
  15. class UploadHandler
  16. {
  17.     private $options;
  18.    
  19.     function __construct($options=null) {
  20.         $this->options = array(
  21.             'script_url' => $_SERVER['PHP_SELF'],
  22.             'upload_dir' => dirname(__FILE__).'/files/',
  23.             'upload_url' => dirname($_SERVER['PHP_SELF']).'/files/',
  24.             'param_name' => 'files',
  25.             // The php.ini settings upload_max_filesize and post_max_size
  26.             // take precedence over the following max_file_size setting:
  27.             'max_file_size' => null,
  28.             'min_file_size' => 1,
  29.             'accept_file_types' => '/.+$/i',
  30.             'max_number_of_files' => null,
  31.             'discard_aborted_uploads' => true,
  32.             'image_versions' => array(
  33.                 // Uncomment the following version to restrict the size of
  34.                 // uploaded images. You can also add additional versions with
  35.                 // their own upload directories:
  36.                 /*
  37.                 'large' => array(
  38.                     'upload_dir' => dirname(__FILE__).'/files/',
  39.                     'upload_url' => dirname($_SERVER['PHP_SELF']).'/files/',
  40.                     'max_width' => 1920,
  41.                     'max_height' => 1200
  42.                 ),
  43.                 */
  44.                 'thumbnail' => array(
  45.                     'upload_dir' => dirname(__FILE__).'/thumbnails/',
  46.                     'upload_url' => dirname($_SERVER['PHP_SELF']).'/thumbnails/',
  47.                     'max_width' => 80,
  48.                     'max_height' => 80
  49.                 )
  50.             )
  51.         );
  52.         if ($options) {
  53.             $this->options = array_merge_recursive($this->options, $options);
  54.         }
  55.     }
  56.    
  57.     private function get_file_object($file_name) {
  58.         $file_path = $this->options['upload_dir'].$file_name;
  59.         if (is_file($file_path) && $file_name[0] !== '.') {
  60.             $file = new stdClass();
  61.             $file->name = $file_name;
  62.             $file->size = filesize($file_path);
  63.             $file->url = $this->options['upload_url'].rawurlencode($file->name);
  64.             foreach($this->options['image_versions'] as $version => $options) {
  65.                 if (is_file($options['upload_dir'].$file_name)) {
  66.                     $file->{$version.'_url'} = $options['upload_url']
  67.                         .rawurlencode($file->name);
  68.                 }
  69.             }
  70.             $file->delete_url = $this->options['script_url']
  71.                 .'?file='.rawurlencode($file->name);
  72.             $file->delete_type = 'DELETE';
  73.             return $file;
  74.         }
  75.         return null;
  76.     }
  77.    
  78.     private function get_file_objects() {
  79.         return array_values(array_filter(array_map(
  80.             array($this, 'get_file_object'),
  81.             scandir($this->options['upload_dir'])
  82.         )));
  83.     }
  84.  
  85.     private function create_scaled_image($file_name, $options) {
  86.         $file_path = $this->options['upload_dir'].$file_name;
  87.         $new_file_path = $options['upload_dir'].$file_name;
  88.         list($img_width, $img_height) = @getimagesize($file_path);
  89.         if (!$img_width || !$img_height) {
  90.             return false;
  91.         }
  92.         $scale = min(
  93.             $options['max_width'] / $img_width,
  94.             $options['max_height'] / $img_height
  95.         );
  96.         if ($scale > 1) {
  97.             $scale = 1;
  98.         }
  99.         $new_width = $img_width * $scale;
  100.         $new_height = $img_height * $scale;
  101.         $new_img = @imagecreatetruecolor($new_width, $new_height);
  102.         switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
  103.             case 'jpg':
  104.             case 'jpeg':
  105.                 $src_img = @imagecreatefromjpeg($file_path);
  106.                 $write_image = 'imagejpeg';
  107.                 break;
  108.             case 'gif':
  109.                 $src_img = @imagecreatefromgif($file_path);
  110.                 $write_image = 'imagegif';
  111.                 break;
  112.             case 'png':
  113.                 $src_img = @imagecreatefrompng($file_path);
  114.                 $write_image = 'imagepng';
  115.                 break;
  116.             default:
  117.                 $src_img = $image_method = null;
  118.         }
  119.         $success = $src_img && @imagecopyresampled(
  120.             $new_img,
  121.             $src_img,
  122.             0, 0, 0, 0,
  123.             $new_width,
  124.             $new_height,
  125.             $img_width,
  126.             $img_height
  127.         ) && $write_image($new_img, $new_file_path);
  128.         // Free up memory (imagedestroy does not delete files):
  129.         @imagedestroy($src_img);
  130.         @imagedestroy($new_img);
  131.         return $success;
  132.     }
  133.    
  134.     private function has_error($uploaded_file, $file, $error) {
  135.         if ($error) {
  136.             return $error;
  137.         }
  138.         if (!preg_match($this->options['accept_file_types'], $file->name)) {
  139.             return 'acceptFileTypes';
  140.         }
  141.         if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  142.             $file_size = filesize($uploaded_file);
  143.         } else {
  144.             $file_size = $_SERVER['CONTENT_LENGTH'];
  145.         }
  146.         if ($this->options['max_file_size'] && (
  147.                 $file_size > $this->options['max_file_size'] ||
  148.                 $file->size > $this->options['max_file_size'])
  149.             ) {
  150.             return 'maxFileSize';
  151.         }
  152.         if ($this->options['min_file_size'] &&
  153.             $file_size < $this->options['min_file_size']) {
  154.             return 'minFileSize';
  155.         }
  156.         if (is_int($this->options['max_number_of_files']) && (
  157.                 count($this->get_file_objects()) >= $this->options['max_number_of_files'])
  158.             ) {
  159.             return 'maxNumberOfFiles';
  160.         }
  161.         return $error;
  162.     }
  163.    
  164.     private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
  165.         $file = new stdClass();
  166.         $file->name = basename(stripslashes($name));
  167.         $file->size = intval($size);
  168.         $file->type = $type;
  169.         $error = $this->has_error($uploaded_file, $file, $error);
  170.         if (!$error && $file->name) {
  171.             if ($file->name[0] === '.') {
  172.                 $file->name = substr($file->name, 1);
  173.             }
  174.             $file_path = $this->options['upload_dir'].$file->name;
  175.             $append_file = is_file($file_path) && $file->size > filesize($file_path);
  176.             clearstatcache();
  177.             if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  178.                 // multipart/formdata uploads (POST method uploads)
  179.                 if ($append_file) {
  180.                     file_put_contents(
  181.                         $file_path,
  182.                         fopen($uploaded_file, 'r'),
  183.                         FILE_APPEND
  184.                     );
  185.                 } else {
  186.                     move_uploaded_file($uploaded_file, $file_path);
  187.                           chmod($file_path,0777);
  188.  
  189.                           $query=mysql_query("INSERT INTO foto_header values ('','$file_path')") or die (mysql_error());
  190.                 }
  191.             } else {
  192.                 // Non-multipart uploads (PUT method support)
  193.                 file_put_contents(
  194.                     $file_path,
  195.                     fopen('php://input', 'r'),
  196.                     $append_file ? FILE_APPEND : 0
  197.                 );
  198.             }
  199.             $file_size = filesize($file_path);
  200.             if ($file_size === $file->size) {
  201.                 $file->url = $this->options['upload_url'].rawurlencode($file->name);
  202.                 foreach($this->options['image_versions'] as $version => $options) {
  203.                     if ($this->create_scaled_image($file->name, $options)) {
  204.                         $file->{$version.'_url'} = $options['upload_url']
  205.                             .rawurlencode($file->name);
  206.                     }
  207.                 }
  208.             } else if ($this->options['discard_aborted_uploads']) {
  209.                 unlink($file_path);
  210.                 $file->error = 'abort';
  211.             }
  212.             $file->size = $file_size;
  213.             $file->delete_url = $this->options['script_url']
  214.                 .'?file='.rawurlencode($file->name);
  215.             $file->delete_type = 'DELETE';
  216.         } else {
  217.             $file->error = $error;
  218.         }
  219.         return $file;
  220.     }
  221.    
  222.     public function get() {
  223.         $file_name = isset($_REQUEST['file']) ?
  224.             basename(stripslashes($_REQUEST['file'])) : null;
  225.         if ($file_name) {
  226.             $info = $this->get_file_object($file_name);
  227.         } else {
  228.             $info = $this->get_file_objects();
  229.         }
  230.         header('Content-type: application/json');
  231.         echo json_encode($info);
  232.     }
  233.    
  234.     public function post() {
  235.         $upload = isset($_FILES[$this->options['param_name']]) ?
  236.             $_FILES[$this->options['param_name']] : array(
  237.                 'tmp_name' => null,
  238.                 'name' => null,
  239.                 'size' => null,
  240.                 'type' => null,
  241.                 'error' => null
  242.             );
  243.         $info = array();
  244.         if (is_array($upload['tmp_name'])) {
  245.             foreach ($upload['tmp_name'] as $index => $value) {
  246.                 $info[] = $this->handle_file_upload(
  247.                     $upload['tmp_name'][$index],
  248.                     isset($_SERVER['HTTP_X_FILE_NAME']) ?
  249.                         $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
  250.                     isset($_SERVER['HTTP_X_FILE_SIZE']) ?
  251.                         $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
  252.                     isset($_SERVER['HTTP_X_FILE_TYPE']) ?
  253.                         $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
  254.                     $upload['error'][$index]
  255.                 );
  256.             }
  257.         } else {
  258.             $info[] = $this->handle_file_upload(
  259.                 $upload['tmp_name'],
  260.                 isset($_SERVER['HTTP_X_FILE_NAME']) ?
  261.                     $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'],
  262.                 isset($_SERVER['HTTP_X_FILE_SIZE']) ?
  263.                     $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'],
  264.                 isset($_SERVER['HTTP_X_FILE_TYPE']) ?
  265.                     $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'],
  266.                 $upload['error']
  267.             );
  268.         }
  269.         header('Vary: Accept');
  270.         if (isset($_SERVER['HTTP_ACCEPT']) &&
  271.             (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
  272.             header('Content-type: application/json');
  273.         } else {
  274.             header('Content-type: text/plain');
  275.         }
  276.         echo json_encode($info);
  277.     }
  278.    
  279.     public function delete() {
  280.         $file_name = isset($_REQUEST['file']) ?
  281.             basename(stripslashes($_REQUEST['file'])) : null;
  282.         $file_path = $this->options['upload_dir'].$file_name;
  283.         $success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
  284.         if ($success) {
  285.            
  286.             foreach($this->options['image_versions'] as $version => $options) {
  287.                 $file = $options['upload_dir'].$file_name;
  288.                 if (is_file($file)) {
  289.                     unlink($file);
  290.                 }
  291.             }
  292.         }
  293.         header('Content-type: application/json');
  294.         echo json_encode($success);
  295.     }
  296. }
  297.  
  298. $upload_handler = new UploadHandler();
  299.  
  300. header('Pragma: no-cache');
  301. header('Cache-Control: private, no-cache');
  302. header('Content-Disposition: inline; filename="files.json"');
  303.  
  304. switch ($_SERVER['REQUEST_METHOD']) {
  305.     case 'HEAD':
  306.     case 'GET':
  307.         $upload_handler->get();
  308.         break;
  309.     case 'POST':
  310.         $upload_handler->post();
  311.         break;
  312.     case 'DELETE':
  313.         $upload_handler->delete();
  314.     //delete
  315.     $query1=mysql_query("DELETE FROM foto_header where file='$file_path')") or die (mysql_error());
  316.         break;
  317.     default:
  318.         header('HTTP/1.0 405 Method Not Allowed');
  319. }
  320. ?>
Advertisement
Add Comment
Please, Sign In to add comment