storage.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreSamples Samples
  7. * @{
  8. */
  9. require_once('./../inc/header.inc.php');
  10. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  11. $oTemplate = BxDolTemplate::getInstance();
  12. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  13. $oTemplate->setPageHeader ("Sample storage");
  14. $oTemplate->setPageContent ('page_main_code', PageCompMainCode());
  15. $oTemplate->getPageCode();
  16. /**
  17. * page code function
  18. */
  19. function PageCompMainCode()
  20. {
  21. ob_start();
  22. ?>
  23. <form enctype="multipart/form-data" method="POST">
  24. Choose a file to upload:
  25. <input name="file" type="file" />
  26. <br />
  27. <input type="submit" name="add" value="Upload File" />
  28. </form>
  29. <?php
  30. $iProfileId = 123;
  31. BxDolStorage::pruning(); // pruning is needed to clear expired security tokens, you can call it on cron when your server is not busy
  32. $oStorage = BxDolStorage::getObjectInstance('sample');
  33. //echo '<pre>reloadMimeTypesFromFile: [' . $oStorage->reloadMimeTypesFromFile(/*'/Users/alex/mime.types'*/'/etc/apache2/mime.types') . ']</pre>';
  34. if (isset($_POST['add'])) {
  35. $iId = $oStorage->storeFileFromForm($_FILES['file'], true, $iProfileId);
  36. if ($iId) {
  37. $iCount = $oStorage->afterUploadCleanup($iId, $iProfileId);
  38. echo "<hr />uploaded file id: " . $iId . "(deleted ghosts:" . $iCount . ")<hr />";
  39. } else {
  40. echo "<hr />error uploading file: " . $oStorage->getErrorString() . "<hr />";
  41. }
  42. }
  43. if (isset($_POST['delete'])) {
  44. foreach ($_POST['file_id'] as $iFileId) {
  45. $bRet = $oStorage->deleteFile($iFileId, $iProfileId);
  46. if ($bRet)
  47. echo "<hr />deleted file id: " . $iFileId . "<hr />";
  48. else
  49. echo "<hr />file deleting error: " . $oStorage->getErrorString() . "<hr />";
  50. }
  51. } elseif (isset($_POST['public']) || isset($_POST['private'])) {
  52. $isPrivate = isset($_POST['private']) && $_POST['private'] ? true : false;
  53. $sAction = $isPrivate ? 'private' : 'public';
  54. foreach ($_POST['file_id'] as $iFileId) {
  55. $bRet = $oStorage->setFilePrivate($iFileId, $isPrivate);
  56. if ($bRet)
  57. echo "<hr /> making <b>$sAction</b> file id: " . $iFileId . "<hr />";
  58. else
  59. echo "<hr />file making <b>$sAction</b> error: " . $oStorage->getErrorString() . "<hr />";
  60. }
  61. }
  62. $a = $oStorage->getFilesAll();
  63. echo "<h2>Files List:</h2> <hr />";
  64. foreach ($a as $r)
  65. echo $r['file_name'] . '(private:[' . $oStorage->isFilePrivate($r['id']) . ']) : <img src="' . $oStorage->getFileUrlById($r['id']) . '" /> <hr />';
  66. echo '<form method="POST">';
  67. foreach ($a as $r)
  68. echo '<input type="checkbox" name="file_id[]" value="' . $r['id'] . '" /> ' . $r['file_name'] . '<br />';
  69. echo '<input type="submit" name="delete" value="Delete" /> <input type="submit" name="public" value="Set Public" /> <input type="submit" name="private" value="Set Private" />';
  70. echo '</form>';
  71. $s = ob_get_clean();
  72. return DesignBoxContent("Sample storage", $s, BX_DB_PADDING_DEF);
  73. }
  74. /** @} */