protected function create() { if (empty($this->newName) || empty($this->file)) { return false; } // return $this; // error_reporting(E_ALL); // ini_set('display_errors', '1'); // echo "<pre>"; // print_r($files); // echo "</pre>"; // echo "<pre>"; // print_r($data); // echo "</pre>"; if (isset($this->tags)) { $person = isset($this->tags->person) ? $this->tags->person : array(); $other = isset($this->tags->other) ? $this->tags->other : array(); $place = isset($this->tags->place) ? $this->tags->place : array(); } $title = isset($this->title) ? $this->title : "No Title"; $author = isset($this->author) ? $this->author : "Unknown"; $comments = $this->description; $newname = $this->newName; $split = explode(".", $this->file->name); $extension = end($split); $savename = $newname . "." . $extension; $link = UPLOAD . "{$savename}"; $temp = File::getByLink($link); // return $temp; if (!$temp) { // return UPLOAD.$savename; $file_path = "upload/" . $savename; if (file_exists($file_path)) { clearstatcache(); //the file already exists return -1; } if ($this->file->error > 0) { //there was an error uploading the file return -2; } else { $result = array(); if (move_uploaded_file($this->file->tmp_name, IMAGEPATH . $link)) { if ($this->file->type == "image/gif" || $this->file->type == "image/jpeg" || $this->file->type == "image/jpg" || $this->file->type == "image/bmp" || $this->file->type == "image/png") { $im = File::thumbnail(IMAGEPATH . $link, 75); $im2 = File::thumbnail(IMAGEPATH . $link, 800); $view_link = UPLOAD . "view/" . $newname . " view." . $extension; $temp_thumblink = UPLOAD . "thumbs/" . $newname . " thumbnail." . $extension; if ($im && $im2) { $imageMade = File::imageToFile($im, IMAGEPATH . $temp_thumblink); if ($imageMade) { $viewMade = File::imageToFile($im2, IMAGEPATH . $view_link); if (!$viewMade) { unlink($link); return -6; } } else { unlink($temp_thumblink); unlink($link); return -6; } } else { unlink($link); return -6; } $type = $this->docType; } else { $view_link = null; $temp_thumblink = "changeMeToDocThumb"; $type = "other"; } $result[] = "Stored in: " . "upload/" . $savename; $init = new File(); $init->id = null; $init->link = $link; $init->thumblink = $temp_thumblink; $init->viewlink = $view_link; $init->title = $title ? $title : "Untitled"; $init->author = $author; $init->comments = $comments; $init->date = null; $init->type = $this->docType; $init_id = $init->save(); if ($init_id) { if (isset($this->tags) && !empty($this->tags)) { foreach ($this->tags as $key => $value) { if ($key === 'person') { foreach ($value as $person) { $tag = new Tag(); $tag->enum = 'person'; $tag->fileid = $init_id; $tag->foreignid = $person->id; $tag->save(); } } else { if ($key === 'place') { foreach ($value as $place) { $tag = new Tag(); $tag->enum = 'place'; $tag->foreignid = false; $tempPlace = Place::getByAll($place); if ($tempPlace) { $tag->foreignid = $tempPlace->id; } else { $tempPlace = recast('Place', $place); $tpId = $tempPlace->save(); if ($tpId) { $tag->foreignid = $tpId; } } if ($tag->foreignid) { $tag->fileid = $init_id; $tag->save(); } } } else { if ($key === 'other') { foreach ($value as $tag) { $tag = recast('Tag', $tag); if ($tag) { $tag->fileid = $init_id; $tag->enum = 'other'; $tag->id = NULL; $tag->save(); } } } } } } } return 1; } else { unlink($temp_thumblink); unlink($link); //database connection wasn't saved return -4; } } else { //file wasn't moved return -3; } } } //the database entry already exists return -5; // save file }
public static function uploadFile($data = NULL, $files = NULL) { // error_reporting(E_ALL); // ini_set('display_errors', '1'); // echo "<pre>"; // print_r($files); // echo "</pre>"; // echo "<pre>"; // print_r($data); // echo "</pre>"; $tags = isset($data['tag']) ? $data['tag'] : array(); $places = isset($data['place']) ? $data['place'] : array(); $title = isset($data['title']) ? $data['title'] : "No Title"; $author = isset($data['author']) ? $data['author'] : "Unknown"; $comments = $data['file_comments']; $newname = $data['newname']; $split = explode(".", $files['file']['name']); $extension = end($split); $savename = $newname . "." . $extension; $link = UPLOAD . "{$savename}"; $temp = File::getByLink($link); // return $temp; if (!$temp) { // return UPLOAD.$savename; $file_path = "upload/" . $savename; if (file_exists($file_path)) { clearstatcache(); //the file already exists return -1; } if ($files["file"]["error"] > 0) { //there was an error uploading the file return -2; } else { $result = array(); if (move_uploaded_file($files["file"]["tmp_name"], "upload/" . $savename)) { if ($files["file"]["type"] == "image/gif" || $files["file"]["type"] == "image/jpeg" || $files["file"]["type"] == "image/jpg" || $files["file"]["type"] == "image/bmp" || $files["file"]["type"] == "image/png") { $im = self::thumbnail($link, 75); $im2 = self::thumbnail($link, 800); $view_link = "upload/view/" . $newname . " view." . $extension; $temp_thumblink = "upload/thumbs/" . $newname . " thumbnail." . $extension; if ($im && $im2) { $imageMade = self::imageToFile($im, $temp_thumblink); if ($imageMade) { $viewMade = self::imageToFile($im2, $view_link); if (!$viewMade) { unlink($link); return -6; } } else { unlink($temp_thumblink); unlink($link); return -6; } } else { unlink($link); return -6; } $type = "image"; } else { $view_link = null; $temp_thumblink = "changeMeToDocThumb"; $type = "other"; } $result[] = "Stored in: " . "upload/" . $savename; $init = new File(); $init->id = null; $init->link = $link; $init->thumblink = $temp_thumblink; $init->viewlink = $view_link; $init->title = $title ? $title : "Untitled"; $init->author = $author; $init->comments = $comments; $init->date = null; $init_id = $init->save(); if ($init_id) { $init->id = $init_id; foreach ($tags as $tag) { $temp_name = explode("||", $tag); if ($temp_name[2] != NULL) { $result[] = saveTags($temp_name[0], "file", intval($init->id), "file_upload", $temp_name[2], $type, URL . $link); } else { $result[] = saveTags($temp_name[0], "file", intval($init->id), "file_upload", -1, $type, URL . $link); } } foreach ($places as $place) { $result[] = savePlaces($place, "file", intval($init->id), "file_upload"); } $init->message = $result; return 1; } else { unlink($temp_thumblink); unlink($link); //database connection wasn't saved return -4; } } else { //file wasn't moved return -3; } } } //the database entry already exists return -5; }