Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function uploadFile()
- {
- $userDetail = getUserDetails();
- $valid_file = false;
- $code = 0;
- //if they DID upload a file...
- if ($_FILES['myFile']['name']) {
- //if no errors...
- if (!$_FILES['myFile']['error']) {
- //now is the time to modify the future file name and validate the file
- $new_file_name = strtolower($_FILES['myFile']['tmp_name']); //rename file
- if ($_FILES['myFile']['size'] > (1024000)) //can't be larger than 1 MB
- {
- $valid_file = false;
- $message = 'Oops! Your file\'s size is to large.';
- }
- //if the file has passed the test
- if ($valid_file) {
- //move it to where we want it to be
- move_uploaded_file($_FILES['myFile']['tmp_name'], 'files/' . $userDetail['user_id'] . "/" . $new_file_name);
- $message = 'Congratulations! Your file was accepted.';
- $code = 1;
- }
- } //if there is an error...
- else {
- //set that to be the returned message
- $message = 'Ooops! Your upload triggered the following error: ' . $_FILES['myFile']['error'];
- respondJSON($code, $message);
- }
- }
- function getFileList()
- {
- setUserDetails(3, "michael");///testing
- $userDetail = getUserDetails();
- $dir_list = scandir('files/' . $userDetail['user_id'] . "/");
- respondJSON(1, $dir_list);
- }
- function getUserDetails()
- {
- $details = array();
- $details['user_id'] = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
- $details['username'] = isset($_SESSION['username']) ? $_SESSION['username'] : null;
- return $details;
- }
- function setUserDetails($user_id, $username)
- {
- $_SESSION['user_id'] = $user_id;
- $_SESSION['username'] = $username;
- }
- function respondJSON($code = 1, $data = array())
- {
- $r = array();
- $r['code'] = $code;
- $r['data'] = $data;
- header("Content-type: application/json");
- print json_encode($r);
- exit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement