Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. function uploadFile()
  2. {
  3.  
  4.  
  5. $userDetail = getUserDetails();
  6. $valid_file = false;
  7.  
  8. $code = 0;
  9. //if they DID upload a file...
  10. if ($_FILES['myFile']['name']) {
  11. //if no errors...
  12. if (!$_FILES['myFile']['error']) {
  13. //now is the time to modify the future file name and validate the file
  14. $new_file_name = strtolower($_FILES['myFile']['tmp_name']); //rename file
  15. if ($_FILES['myFile']['size'] > (1024000)) //can't be larger than 1 MB
  16. {
  17. $valid_file = false;
  18. $message = 'Oops! Your file\'s size is to large.';
  19. }
  20.  
  21. //if the file has passed the test
  22. if ($valid_file) {
  23. //move it to where we want it to be
  24. move_uploaded_file($_FILES['myFile']['tmp_name'], 'files/' . $userDetail['user_id'] . "/" . $new_file_name);
  25. $message = 'Congratulations! Your file was accepted.';
  26. $code = 1;
  27. }
  28. } //if there is an error...
  29. else {
  30. //set that to be the returned message
  31. $message = 'Ooops! Your upload triggered the following error: ' . $_FILES['myFile']['error'];
  32. respondJSON($code, $message);
  33. }
  34.  
  35. }
  36.  
  37. function getFileList()
  38. {
  39.  
  40.  
  41. setUserDetails(3, "michael");///testing
  42. $userDetail = getUserDetails();
  43.  
  44.  
  45. $dir_list = scandir('files/' . $userDetail['user_id'] . "/");
  46.  
  47.  
  48. respondJSON(1, $dir_list);
  49.  
  50. }
  51.  
  52. function getUserDetails()
  53. {
  54. $details = array();
  55.  
  56. $details['user_id'] = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
  57.  
  58. $details['username'] = isset($_SESSION['username']) ? $_SESSION['username'] : null;
  59.  
  60. return $details;
  61. }
  62.  
  63. function setUserDetails($user_id, $username)
  64. {
  65. $_SESSION['user_id'] = $user_id;
  66. $_SESSION['username'] = $username;
  67. }
  68.  
  69.  
  70. function respondJSON($code = 1, $data = array())
  71. {
  72. $r = array();
  73.  
  74. $r['code'] = $code;
  75. $r['data'] = $data;
  76.  
  77.  
  78. header("Content-type: application/json");
  79. print json_encode($r);
  80.  
  81. exit;
  82.  
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement