1

I am trying to read file content of (.stl) file, from an external link, my code is as bellow

 if($request->filled('link')) {

            $file = file_get_contents($request->link);
            $file_name = now()->format('YmdHi'). rand(12321, 45654664) . '.stl';
            file_put_contents(public_path('model-files/' . $file_name), $file);
 }

However everthing works fine, and i am not getting any code error, but when i tries to open the file, the file do not open, saying "File can not be opened". If i open the file link directly in the browser, it downloads and open just fine, also the file size of the file stored by code is also not the same to original link file, so i am not sure if all content of file is read properly or not, please guide

273K
  • 29,503
  • 10
  • 41
  • 64
user8076689
  • 127
  • 1
  • 9
  • is this "stl" file refers to CAD related file format? *"also the file size of the file stored by code is also not the same to original link file"* - does downloading the file involves logging in? have you tried to open the file in notepad (you might see a bunch of html instead of proper "stl" file)? – Bagus Tesa May 01 '22 at 08:58
  • stl files are basically 3d model files, and it do not require logging in, it is more of a export url, also when i open the content of the files they are not the same ( i mean the original file and the file that i create) – user8076689 May 01 '22 at 09:02
  • so.. you see.. if the download process involves login, redirection, or some other stuff, `file_get_content` might not up to the task. try [use curl](https://stackoverflow.com/a/9074272), see if it works. and thats why i wish you could get an educated guess on what you actually download, so the problem is much clear. – Bagus Tesa May 01 '22 at 09:15
  • sure, i will try curl, would you like to have the export link, if you would like to try? it is basically a temporary url, that will expire after sometime, thats why i want to save it – user8076689 May 01 '22 at 09:36
  • if its publicly available stuff and long lived then sure. adding it to the question will help people debug your problem. however, given its temporary in nature.. – Bagus Tesa May 01 '22 at 10:13
  • Did you try to use `\Storage::put('/model-files/'.$file_name, $file);` instead of `file_put_contents` ? or are you facing OpenSSl issues? – Mohamed Mo Kawsara May 02 '22 at 05:06
  • I actually tried CURL and Storage::put both the options, but the files are still not working as expected. @BagusTesa. here is the link to the modal export url https://sdeuc1.eu-central-1.shapediver.com/session/6ba87662-697b-4268-ba26-90ecf544ee5f/export/8003a06ec43a7d12eee710b7c98173795622aab32d79053c65ad3398cd3ac4df2ab34c9a8e61a956a82113d1d819b716dcd57255af4a56a77db2539fe7405cf41c28176fb2841bdea72ae4f8e02bd0c87fec3391f906eea152a11d59f2571fa6511a766d8fb2ed965c4f1e9bb06a9b10d5854776189e231c07a573e6e4fb257258d1fb1dd068cdc79d15b17ea6ee25aa367de7f9b931-36626138373636322d363937622d3432 – user8076689 May 03 '22 at 17:10
  • You will see, when you open the link, it will download the file just fine, but not working well in case of saving file using code – user8076689 May 03 '22 at 17:11
  • @user8076689 before you start download on browser, try open browser console's network tab first and see the interaction there. it would be really helpful if you could identify the failed download's content though.. – Bagus Tesa May 04 '22 at 08:19

0 Answers0