/** * Get a curl instance for the file to initiate download, if url exists. * * @param File $downloadFile * @return Curl */ public function setCurlForFile(File &$downloadFile) { $urlInfo = $this->infoFromURL($downloadFile->getUrl()); if ($urlInfo->isSuccessful()) { $downloadFile->setInfo($urlInfo); $fileSize = null; if (file_exists($downloadFile->getDestination())) { $fileSize = $this->getLocalFileSize($downloadFile->getDestination()); if (!$downloadFile->getOverwrite() && $fileSize >= $urlInfo->info->CONTENT_LENGTH_DOWNLOAD) { return true; } } $curl = new Curl($downloadFile->getUrl()); $curl->options->SSL_VERIFYPEER = false; $curl->options->FOLLOWLOCATION = true; $curl->options->RETURNTRANSFER = false; $curl->options->CONNECTTIMEOUT = $this->connectTimeout; $curl->options->TIMEOUT = $this->curlTimeout; if (!$downloadFile->getOverwrite() && file_exists($downloadFile->getDestination()) && $urlInfo->headers->acceptRanges == 'bytes') { $curl->options->FILE = fopen($downloadFile->getDestination(), 'a'); $curl->options->RESUME_FROM = $fileSize; $this->logMessage("Resuming {$downloadFile->getUrl()} From {$fileSize} Bytes"); } else { $curl->options->FILE = fopen($downloadFile->getDestination(), 'w'); } if ($this->cookiePath && $this->cookieFile) { $this->cookieFile = $this->cookiePath . "{$this->cookieFile}"; $curl->options->COOKIEFILE = $this->cookieFile; $curl->options->COOKIEJAR = $this->cookieFile; } $headers = array('DNT: 1'); $curl->options->HTTPHEADER = $headers; $downloadFile->setCurl($curl); return true; } else { if ($urlInfo->isForbidden()) { //Forbidden throw new Exception("Forbidden Access to the URL."); } else { throw new Exception("General Failure to access the URL."); } } return false; }