You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. // Init owncloud
  3. // Firefox and Konqueror tries to download application/json for me. --Arthur
  4. OCP\JSON::setContentTypeHeader('text/plain');
  5. OCP\JSON::checkLoggedIn();
  6. OCP\JSON::callCheck();
  7. $l = OC_L10N::get('files');
  8. // get array with current storage stats (e.g. max file size)
  9. $storageStats = \OCA\files\lib\Helper::buildFileStorageStatistics($dir);
  10. if (!isset($_FILES['files'])) {
  11. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('No file was uploaded. Unknown error')), $storageStats)));
  12. exit();
  13. }
  14. foreach ($_FILES['files']['error'] as $error) {
  15. if ($error != 0) {
  16. $errors = array(
  17. UPLOAD_ERR_OK => $l->t('There is no error, the file uploaded with success'),
  18. UPLOAD_ERR_INI_SIZE => $l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini: ')
  19. . ini_get('upload_max_filesize'),
  20. UPLOAD_ERR_FORM_SIZE => $l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified'
  21. . ' in the HTML form'),
  22. UPLOAD_ERR_PARTIAL => $l->t('The uploaded file was only partially uploaded'),
  23. UPLOAD_ERR_NO_FILE => $l->t('No file was uploaded'),
  24. UPLOAD_ERR_NO_TMP_DIR => $l->t('Missing a temporary folder'),
  25. UPLOAD_ERR_CANT_WRITE => $l->t('Failed to write to disk'),
  26. );
  27. OCP\JSON::error(array('data' => array_merge(array('message' => $errors[$error]), $storageStats)));
  28. exit();
  29. }
  30. }
  31. $files = $_FILES['files'];
  32. $dir = $_POST['dir'];
  33. $error = '';
  34. $totalSize = 0;
  35. foreach ($files['size'] as $size) {
  36. $totalSize += $size;
  37. }
  38. if ($totalSize > OC_Filesystem::free_space($dir)) {
  39. OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Not enough storage available')), $storageStats)));
  40. exit();
  41. }
  42. $result = array();
  43. if (strpos($dir, '..') === false) {
  44. $fileCount = count($files['name']);
  45. for ($i = 0; $i < $fileCount; $i++) {
  46. $target = OCP\Files::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
  47. // $path needs to be normalized - this failed within drag'n'drop upload to a sub-folder
  48. $target = OC_Filesystem::normalizePath($target);
  49. if (is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) {
  50. $meta = OC_FileCache::get($target);
  51. $id = OC_FileCache::getId($target);
  52. // updated max file size after upload
  53. $storageStats = \OCA\files\lib\Helper::buildFileStorageStatistics($dir);
  54. $result[] = array_merge(array('status' => 'success',
  55. 'mime' => $meta['mimetype'],
  56. 'size' => $meta['size'],
  57. 'id' => $id,
  58. 'name' => basename($target)), $storageStats
  59. );
  60. }
  61. }
  62. OCP\JSON::encodedPrint($result);
  63. exit();
  64. } else {
  65. $error = $l->t('Invalid directory.');
  66. }
  67. OCP\JSON::error(array('data' => array_merge(array('message' => $error), $storageStats)));