Example #1
0
 /**
  * Get the real extension of a file despite of the extension writted in his name
  *
  * @param $file
  *
  * @return bool|mixed
  */
 public static function getRealExtension($file)
 {
     if (!is_file($file)) {
         return false;
     }
     $info = new \finfo(FILEINFO_MIME_TYPE);
     $mime = $info->file($file);
     return MimeToExtension::wrapper()->getExtensionFromMime($mime);
 }
Example #2
0
 protected function megaDL(string $uri, string $destination = null, string $filename = null, bool $overwrite = false)
 {
     $command = "megadl {$uri} --no-progress --print-names";
     $filepath = '';
     if (null !== $destination || null !== $filename) {
         if (null !== $destination) {
             if (false === is_dir($destination)) {
                 mkdir($destination, 0755, true);
             }
             $destination = substr($destination, -1) === '/' ? $destination : "{$destination}/";
             $filepath .= $destination;
         }
         if (null !== $filename) {
             $filepath .= $filename[0] === '/' ? substr($filename, 1) : $filename;
             if (false === $overwrite) {
                 $i = 0;
                 while (file_exists($filepath)) {
                     ++$i;
                     $j = $i - 1;
                     $buffer = $filename;
                     $str = str_replace("(renamed_{$j})", "(renamed_{$i})", $buffer);
                     if ($filename !== $str) {
                         $filename = $str;
                     } else {
                         $filename = $filename . "(renamed_{$i})." . pathinfo($filepath, PATHINFO_EXTENSION);
                     }
                     $filepath = "{$destination}/{$filename}";
                 }
             } else {
                 unlink($filepath);
             }
         } else {
             $filename = uniqid(mt_rand());
             $filepath .= $filename;
         }
     } else {
         $filename = uniqid(mt_rand());
         $filepath .= $filename;
     }
     $command .= " --path={$filepath}";
     $command = escapeshellcmd($command) . ' 2>&1';
     // Escape command & display errors
     putenv('LANG=fr_FR.UTF-8');
     // fix issues with french accents.
     exec($command, $output);
     if ('Le fichier existe' === substr($output[0], -17)) {
         // If the file already exists
         return $this->megaDL($uri, $destination, $filename, $overwrite);
     } elseif ('ERROR' !== substr($output[0], 0, 5)) {
         // If the file was correctly downloaded
         $extension = pathinfo($filepath, PATHINFO_EXTENSION);
         $mimetype = mime_content_type($filepath);
         if (empty($extension)) {
             $extension = MimeToExtension::wrapper()->getExtensionFromMime($mimetype);
             $newpath = "{$filepath}.{$extension}";
             rename($filepath, $newpath);
             return ['path' => $newpath, 'name' => $filename, 'extension' => $extension, 'mimetype' => $mimetype, 'created' => new \DateTime(), 'size' => filesize($newpath)];
         }
         return ['path' => $filepath, 'name' => $filename, 'extension' => $extension, 'mimetype' => $mimetype, 'created' => new \DateTime(), 'size' => filesize($filepath)];
     } else {
         return false;
     }
 }
Example #3
0
 /**
  * @param $uri
  * @return array|bool
  */
 private function tryFileGetContents($uri)
 {
     try {
         if (false !== ($this->headers = @get_headers($uri))) {
             if ($this->isAuthorized($uri)) {
                 $this->filename = $this->getFilenameFromHttpHeaders($this->headers);
                 $this->file = file_get_contents($uri);
                 $finfo = new \finfo(FILEINFO_MIME_TYPE);
                 $this->extension = MimeToExtension::wrapper()->getExtensionFromMime($finfo->buffer($this->file));
                 return true;
             } else {
                 throw new \Exception('Unauthorized to access to this file.');
             }
         } else {
             return false;
         }
     } catch (\Exception $e) {
         echo "file_get_contents() error (code: {$e->getCode()}): {$e->getMessage()} (In file: {$e->getFile()}:{$e->getLine()}))";
         return false;
     }
 }