示例#1
0
 /**
  * Save the current torrent object to the filename specified
  *
  * This method will save the current object to a file. If the file specified exists it will be overwritten.
  *
  * @param string $filename
  * @param boolean $write
  * @throws PHP_BitTorrent_Torrent_Exception
  * @return PHP_BitTorrent_Torrent
  */
 public function save($filename, $write = true)
 {
     if (!is_writable($filename) && !is_writable(dirname($filename))) {
         throw new PHP_BitTorrent_Torrent_Exception('Could not open file "' . $filename . '" for writing.');
     }
     $announce = $this->getAnnounce();
     if (empty($announce)) {
         throw new PHP_BitTorrent_Torrent_Exception('Announce URL is missing.');
     }
     $info = $this->getInfo();
     if (empty($info)) {
         throw new PHP_BitTorrent_Torrent_Exception('The info part of the torrent is empty.');
     }
     $createdAt = $this->getCreatedAt();
     if (empty($createdAt)) {
         $createdAt = time();
     }
     $torrent = array('announce' => $announce, 'creation date' => $createdAt, 'info' => $info);
     if (($comment = $this->getComment()) !== null) {
         $torrent['comment'] = $comment;
     }
     if (($createdBy = $this->getCreatedBy()) !== null) {
         $torrent['created by'] = $createdBy;
     }
     // Create the encoded dictionary
     $dictionary = PHP_BitTorrent_Encoder::encodeDictionary($torrent);
     // Write the encoded data to the file
     if ($write) {
         file_put_contents($filename, $dictionary);
     } else {
         return $dictionary;
     }
     return $this;
 }