public function saveImage($isbn = null, $url = null) { if ($isbn == null || $url == null || $url == '') { return false; } $http = new HttpSocket(); try { $f = fopen(WWW_ROOT . 'img' . DS . 'books' . DS . $isbn . '.png', 'w'); $http->setContentResource($f); $http->get($url); fclose($f); } catch (Exception $e) { return false; } return 'books/' . $isbn . '.png'; }
private function __download_attachments() { //get HttpSocket App::uses('HttpSocket', 'Network/Http'); //get Folder App::uses('Folder', 'Utility'); //get user id $userId = AuthComponent::user('id'); foreach ($this->__attachments as $attachment) { //generate Post model $Post = ClassRegistry::init('CloggyBlogPost'); //generate Media model $Media = ClassRegistry::init('CloggyBlogMedia'); $postId = ''; foreach ($this->__posts as $post) { if ($attachment['post_id'] == $post['post_id']) { $detailPost = $Post->getPostIdByTitle($post['title']); if ($detailPost) { $postId = $detailPost['CloggyNodeSubject']['node_id']; break; } } } /* * only if post id is not empty */ if (!empty($postId)) { /* * generate data */ $imageUrl = $attachment['url']; $exp = explode('.', $imageUrl); $ext = end($exp); $filename = $attachment['title'] . '.' . $ext; $path = APP . 'Plugin' . DS . 'Cloggy' . DS . 'webroot' . DS . 'uploads' . DS . 'CloggyBlog' . DS . 'images' . DS . $postId . DS; $filepath = $path . $filename; /* * create folder */ $dir = new Folder(); $dir->create($path); /* * download atttachment */ $http = new HttpSocket(); $f = fopen($filepath, 'w+'); $http->setContentResource($f); $results = $http->get($imageUrl); fclose($f); /* * save image */ $mediaId = $Media->setImage($userId, array('media_file_type' => $results->getHeader('Content-Type'), 'media_file_location' => DS . 'uploads' . DS . 'CloggyBlog' . DS . 'images' . DS . $postId . DS . $filename)); //save new image $Media->setPostAttachment($postId, $mediaId); } } }