file_store.class.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. *
  4. * @copyright (c) 2012 University of Geneva
  5. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  6. * @author Laurent Opprecht <laurent@opprecht.info>
  7. */
  8. class FileStore
  9. {
  10. /**
  11. *
  12. * @param int $c_id
  13. * @param string $sub_path
  14. * @return FileStore
  15. */
  16. static function course($c_id, $sub_path = '')
  17. {
  18. $sys_path = api_get_path(SYS_COURSE_PATH);
  19. $course = api_get_course_info_by_id($c_id);
  20. $course_path = $course['path'];
  21. $path = $sys_path.$course_path.$sub_path;
  22. if (!is_dir($path)) {
  23. $mode = api_get_permissions_for_new_directories();
  24. $success = mkdir($path, $mode, true);
  25. if (!$success) {
  26. return false;
  27. }
  28. }
  29. return new self($path);
  30. }
  31. protected $root = '';
  32. public function __construct($root)
  33. {
  34. $root = ltrim($root, '/');
  35. $root .= '/';
  36. $this->root = $root;
  37. }
  38. public function root()
  39. {
  40. return $this->root;
  41. }
  42. function accept($filename)
  43. {
  44. return (bool)FileManager::filter_extension($filename);
  45. }
  46. function add($path)
  47. {
  48. $root = $this->root();
  49. $id = $this->new_id();
  50. $new_path = "$root/$id";
  51. $success = @move_uploaded_file($path, $new_path);
  52. return $success ? $id : false;
  53. }
  54. function remove($path)
  55. {
  56. $root = $this->root();
  57. $full_path = "$root/$path";
  58. if (is_file($full_path)) {
  59. $result = unlink($full_path);
  60. return $result;
  61. }
  62. return false;
  63. }
  64. function get($id)
  65. {
  66. $root = $this->root();
  67. $result = "$root/$id";
  68. return $result;
  69. }
  70. function new_id()
  71. {
  72. $root = $this->root();
  73. $id = uniqid('');
  74. $path = "$root/$id";
  75. while (file_exists($path)) {
  76. $id = uniqid('');
  77. $path = "$root/$id";
  78. }
  79. return $id;
  80. }
  81. }