github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/app/lib/plupload-2.1.2/examples/upload.php (about)

     1  <?php
     2  /**
     3   * upload.php
     4   *
     5   * Copyright 2013, Moxiecode Systems AB
     6   * Released under GPL License.
     7   *
     8   * License: http://www.plupload.com/license
     9   * Contributing: http://www.plupload.com/contributing
    10   */
    11  
    12  #!! IMPORTANT: 
    13  #!! this file is just an example, it doesn't incorporate any security checks and 
    14  #!! is not recommended to be used in production environment as it is. Be sure to 
    15  #!! revise it and customize to your needs.
    16  
    17  
    18  // Make sure file is not cached (as it happens for example on iOS devices)
    19  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    20  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    21  header("Cache-Control: no-store, no-cache, must-revalidate");
    22  header("Cache-Control: post-check=0, pre-check=0", false);
    23  header("Pragma: no-cache");
    24  
    25  /* 
    26  // Support CORS
    27  header("Access-Control-Allow-Origin: *");
    28  // other CORS headers if any...
    29  if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    30  	exit; // finish preflight CORS requests here
    31  }
    32  */
    33  
    34  // 5 minutes execution time
    35  @set_time_limit(5 * 60);
    36  
    37  // Uncomment this one to fake upload time
    38  // usleep(5000);
    39  
    40  // Settings
    41  $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
    42  //$targetDir = 'uploads';
    43  $cleanupTargetDir = true; // Remove old files
    44  $maxFileAge = 5 * 3600; // Temp file age in seconds
    45  
    46  
    47  // Create target dir
    48  if (!file_exists($targetDir)) {
    49  	@mkdir($targetDir);
    50  }
    51  
    52  // Get a file name
    53  if (isset($_REQUEST["name"])) {
    54  	$fileName = $_REQUEST["name"];
    55  } elseif (!empty($_FILES)) {
    56  	$fileName = $_FILES["file"]["name"];
    57  } else {
    58  	$fileName = uniqid("file_");
    59  }
    60  
    61  $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
    62  
    63  // Chunking might be enabled
    64  $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
    65  $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
    66  
    67  
    68  // Remove old temp files	
    69  if ($cleanupTargetDir) {
    70  	if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
    71  		die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
    72  	}
    73  
    74  	while (($file = readdir($dir)) !== false) {
    75  		$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
    76  
    77  		// If temp file is current file proceed to the next
    78  		if ($tmpfilePath == "{$filePath}.part") {
    79  			continue;
    80  		}
    81  
    82  		// Remove temp file if it is older than the max age and is not the current file
    83  		if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
    84  			@unlink($tmpfilePath);
    85  		}
    86  	}
    87  	closedir($dir);
    88  }	
    89  
    90  
    91  // Open temp file
    92  if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
    93  	die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
    94  }
    95  
    96  if (!empty($_FILES)) {
    97  	if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
    98  		die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
    99  	}
   100  
   101  	// Read binary input stream and append it to temp file
   102  	if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
   103  		die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
   104  	}
   105  } else {	
   106  	if (!$in = @fopen("php://input", "rb")) {
   107  		die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
   108  	}
   109  }
   110  
   111  while ($buff = fread($in, 4096)) {
   112  	fwrite($out, $buff);
   113  }
   114  
   115  @fclose($out);
   116  @fclose($in);
   117  
   118  // Check if file has been uploaded
   119  if (!$chunks || $chunk == $chunks - 1) {
   120  	// Strip the temp .part suffix off 
   121  	rename("{$filePath}.part", $filePath);
   122  }
   123  
   124  // Return Success JSON-RPC response
   125  die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');