- // Store the file's full file path in memory
- $uploadFile = UGUU_FILES_ROOT . $newname;
-
- // Attempt to move it to the static directory
- if (!move_uploaded_file($file->tempfile, $uploadFile)) {
- http_response_code(500);
- throw new Exception(
- 'Failed to move file to destination',
- 500
- ); // HTTP status code "500 Internal Server Error"
- }
-
- // Need to change permissions for the new file to make it world readable
- if (!chmod($uploadFile, 0644)) {
- http_response_code(500);
- throw new Exception(
- 'Failed to change file permissions',
- 500
- ); // HTTP status code "500 Internal Server Error"
- }
-
- // Add it to the database
- if(LOG_IP == 'yes'){
- $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ip) VALUES (:hash, :orig, :name, :size, :date, :ip)');
- }else{
- $ip = '0';
- $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ip) VALUES (:hash, :orig, :name, :size, :date, :ip)');
- }
- // Common parameters binding
- $q->bindValue(':hash', $file->getSha1(), PDO::PARAM_STR);
- $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR);
- $q->bindValue(':name', $newname, PDO::PARAM_STR);
- $q->bindValue(':size', $file->size, PDO::PARAM_INT);
- $q->bindValue(':date', time(), PDO::PARAM_INT);
- $q->bindValue(':ip', $ip, PDO::PARAM_STR);
- $q->execute();
-
- return array(
- 'hash' => $file->getSha1(),
- 'name' => $file->name,
- 'url' => UGUU_URL.rawurlencode($newname),
- 'size' => $file->size,
- );
-}
-
-/**
- * Reorder files array by file.
- *
- * @param $_FILES
- *
- * @return array
- */
-function diverseArray($files)
-{
- $result = array();
-
- foreach ($files as $key1 => $value1) {
- foreach ($value1 as $key2 => $value2) {
- $result[$key2][$key1] = $value2;
- }
- }
-
- return $result;
-}
-
-/**
- * Reorganize the $_FILES array into something saner.
- *
- * @param $_FILES
- *
- * @return array
- */
-function refiles($files)
-{
- $result = array();
- $files = diverseArray($files);
-
- foreach ($files as $file) {
- $f = new UploadedFile();
- $f->name = $file['name'];
- $f->mime = $file['type'];
- $f->size = $file['size'];
- $f->tempfile = $file['tmp_name'];
- $f->error = $file['error'];
- $result[] = $f;
- }
-
- return $result;
-}
-
-$type = isset($_GET['output']) ? $_GET['output'] : 'json';
-$response = new Response($type);