Source for file RSFile.php
Documentation is available at RSFile.php
* Simple rapidshare file checker class.
* $files = RSFile::getInstance($id1, $name1);
* $files = RSFile::getInstance($id2, $name2);
* $files->processFiles();
* if ($files->getStatus() === RSFile::STATUS_FILE_OK) {
* echo $files->getSize(RSFile::SIZE_MB);
* echo $files->getDownloadMirror();
class RSFile implements Iterator {
* The file was not found on the rapidshare servers.
const STATUS_FILE_NOT_FOUND = 0;
* File was found and everything is OK.
const STATUS_FILE_OK = 1;
* The rapidshare server is down.
const STATUS_SERVER_DOWN = 3;
* The file was marked as illegal.
const STATUS_ILLEGAL_FILE = 4;
* Format file size to bytes (by default).
* Format file size to kilobytes
* Format file size to megabytes
* Format file size to gigabytes
* The maximum number of calls to the rapidshare servers.
const MAX_SERVER_CALLS = 3;
* Stores the latest error (if any)
* Stores the latest error number (if any)
* Stores the current file id
* Stores the current file name
* Stores the current file size in bytes
* Stores the server id for the current file
* Stores the status for the current file
* Stores the short host for the current file
* Stores the md5sum of the current file
* We use this simple variable to know if more than MAX_SERVER_CALLS calls
* are made to the rapidshare server, you should never make more than one
* call to processFiles() function that connects to the rapidshare servers.
private static $calls = 0;
* The list of instances of this class
private static $instances = array();
* Returns only one instance of this class per id/name
* @param string $id File id on rapidshare servers
* @param string $name File name on rapidshare servers
$instance = (string) $id. $name;
if (!isset (self::$instances[$instance]) || !self::$instances[$instance] instanceof self)
self::$instances[$instance] = new self($id, $name);
return self::$instances[$instance];
trigger_error("Empty instance id!", E_USER_ERROR);
$instance = (string) $id. $name;
if (isset (self::$instances[$instance]))
unset (self::$instances[$instance]);
* Returns the id for the current file
* Returns the name of the current file
* Returns the size of the current file
* @param string $format Use one of the defined SIZE_* constants
* @param int $precision Precision for round function
* @return float The file size changed in the desired format.
public function getFileSize($format = self::SIZE_B, $precision = 2) {
$precision = (int) $precision;
return round($this->_size / (1024 * 1024 * 1024), $precision);
return round($this->_size / (1024 * 1024), $precision);
* Returns the server id where the file is hosted.
* Returns the status of the current file
* Returns the short host for the current file, is used to get the best
* Returns the best download mirror for the current file or null if the
* file status is not equal to 1
// The status of the file MUST be ok or there is no file.
// http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename
$url .= '.rapidshare.com/files/';
* Returns the md5sum of the current file
* Simple implode function but works with multiton objects.
* @param string $glue String separator
* @param object $objs Instance of class implementing Iterator interface
* @param string $callback Object method used to get the string for
foreach ($objs as $obj) {
$returns .= $obj->$callback();
* If the separator is not an empty string then remove it from the end
* This function must be called only after every file is already created.
* e.g. $files = RSFile::getInstance(id1, name1)->getInstance(id2, name2)...
* $files->processFiles();
* foreach($files as $file)
* $file->getFileSize() ...
* @return boolean true on success and false otherwise.
// Make sure we dont call to many times the rapidshare api.
if (self::$calls >= self::MAX_SERVER_CALLS) {
$this->_error = 'You can NOT call this method more than
MAX_SERVER_CALLS times!';
// Get file ids separated by comma
// Get file names separated by comma
$filenames = $this->implodeObjs(',', $this, 'getFileName');
// Some checking before getting any error from the server.
if (count($files) > 3000 || count($filenames) > 30000) {
$this->_error = 'There is a server based limitation. Files are limited
to 3000 characters and filenames to 30.000';
// Create all POST fields
$chPostFields = 'sub=checkfiles';
$chPostFields .= '&files=';
$chPostFields .= '&filenames=';
$chPostFields .= $filenames;
$ch = curl_init('http://api.rapidshare.com/cgi-bin/rsapi.cgi');
// Get all files from the response
// There was an error. Take note of that...
// Return false so we know ther is an error.
foreach ($files as $file) {
list ($id, $name, $size, $serverId, $status, $shortHost, $md5sum)
self::$instances[$id. $name]->_size = (int) $size;
self::$instances[$id. $name]->_serverId = (int) $serverId;
self::$instances[$id. $name]->_status = (int) $status;
self::$instances[$id. $name]->_shortHost = $shortHost;
self::$instances[$id. $name]->_md5sum = $md5sum;
* Returns the last error description.
public function error() {
* Returns the last error number.
public function errno() {
* No serialization allowed
return key(self::$instances);
return next(self::$instances);
public function valid() {
return current(self::$instances) !== false;
|