0

I have a string like https://filemanager.elo.io/shop/fc1430db9a.ogg in my database that i pass to my API. In the api, i try to convert the string to file but get the error

Call to a member function guessExtension() on resource

API.php

public function testAPI(Request $request)
{
   //receive media string or url from database

   $media_url = $request->get('media_url');
   $media_file = fopen($media_url,'r');
   $media_file->guessExtension();
}

What could i be doing wrong ? Could it be my url is not converted to file ?

PS: New to PHP & Laravel

AMAPHP
  • 1
  • Can you dump out what you get into the $media_file obj? – Kingalione Aug 17 '20 at 10:28
  • Does this answer your question? [Download File to server from URL](https://stackoverflow.com/questions/3938534/download-file-to-server-from-url) – Dmitry Aug 17 '20 at 11:23

1 Answers1

0

Your media_url is not a file it's a string so you cannot use ->guessExtension() on string, you can get extension from string like so

$path_info = pathinfo($media_url);

if you dd($path_info) you will see

[
  "dirname" => "https://filemanager.elo.io/shop"
  "basename" => "fc1430db9a.ogg"
  "extension" => "ogg"
  "filename" => "fc1430db9a"
]

now you can get the extension by doing $path_info['extension']

or you simply can do

pathinfo($media_url, PATHINFO_EXTENSION);

this will return the extension directly, you can read more about pathinfo() Here

mmabdelgawad
  • 1,922
  • 2
  • 4
  • 11
  • Well he is not using ->guessExtension() on the $media_url he is using it on the $media_file which looks ok to me. – Kingalione Aug 17 '20 at 10:28
  • `fopen()` will return a handle to the file which doesn't have a method called `guessExtension()` it should be an object of `UploadedFile` class so you can call `guessExtension()`, either ways if he want to get the extension from the file he can get it from the url if the file will have the same extension of the url. – mmabdelgawad Aug 17 '20 at 10:38