-2

my link (URL) is different!!! and does not work with usual method I think because site load with js or aspx you can test my link (URL) in your browser and see download starting but cant work in php

I have tested all methods (fetch, curl, file, get, put), but it does not work. I have a similar URL here: 'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'

I can open it in the browser and download a csv file I need to do this in php and save the csv file on server

Here is what I have tried so far:

<?php

$file = fopen('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
$file = file_get_contents('http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0');
file_put_contents('ClientTypeAll.csv', $file);

?>

I do not want Contents !!! I want a csv file form my link if you test my link in your browser download start in your pc

  • `$file_put_contents()`??? You meant `file_put_contents()` – brombeer Dec 09 '22 at 16:15
  • 2
    [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) might be worth a read – brombeer Dec 09 '22 at 16:15
  • "_you can test my link (URL) in your browser_" How, when "_my link (URL) is different!!!_"? That URL does nothing for me – brombeer Dec 09 '22 at 16:17
  • 1-thanks brombeer , i forced use $ stackoverflow dont accept ! – chrome chrome Dec 09 '22 at 16:22
  • 'code' http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0 'code' if test in your browser download start but in php does not work can you run this site in your location ? www.tsetmc.com – chrome chrome Dec 09 '22 at 16:24
  • this site work with deep js and aspx , in browser load all part , but in php dont full load – chrome chrome Dec 09 '22 at 16:25
  • As I said, that url does nothing for me. Takes too long. No error. I aborted after 15 seconds – brombeer Dec 09 '22 at 16:26
  • maybe tset this site ' www.tsetmc.com' its work for me very fast – chrome chrome Dec 09 '22 at 16:28
  • Nope. Nothing. Since you misspelled `test` as `tset` in your previous comment I tried `www.testmc.com` - which brought up a (japanese?) site. Could that be the problem? A typo in your url? – brombeer Dec 09 '22 at 16:32
  • @brombeer it's actually Korean, but all the same, OP's URL doesn't load for me either, and the testmc site doesn't prompt anything. OP you should start by opening up your browser's developer tools and studying what's actually happening. Chances are the CSV's URL will show up in your network tab. – Markus AO Dec 09 '22 at 16:38
  • tanks bro , no , my address is true , tse , tmc , its Tehran Securities Exchange Technology Management Co , in iran – chrome chrome Dec 09 '22 at 16:39
  • i think goverment try to block other ip from iran and doesnot load page for your ip , but tank you bro – chrome chrome Dec 09 '22 at 16:41
  • _Tsetmc.com is DOWN for everyone. It is not just you. The server is not responding..._ ... I'm guessing they may restrict international access? – Markus AO Dec 09 '22 at 16:41
  • its work fast for me and all ip from iran , my goverment try too make local internet simmilar china and doesnot upcomming for others ip ,,, you can run this site only with vpn vps in iran , – chrome chrome Dec 09 '22 at 16:45

2 Answers2

0

I run this code with a remote PDF file.

<?php
$url = 'https://example.com/file.pdf';
$dir_name = 'storage-x'; // saVe the directory name

if (!file_exists($dir_name)) {
    mkdir($dir_name, 0777, true);
}
$path = $dir_name.'/'.rand().'.pdf';
$file = file_get_contents($url);
file_put_contents($path, $file);

?>
Houssem Cherif
  • 221
  • 1
  • 11
0

Please follow the below step.

  • Get file form URL.
  • Set directory and check file exit condition and update directory access permission.
  • Set new file path, name, and directory.
  • Save the file.

Please check the below example which works for me.

<?php
     $file = file_get_contents('https://example.com/file.pdf');
     
     $dirName = 'storage-pdf';

     if (!file_exists($dirName)) {
         mkdir($dirName, 0777, true);
     }

     $newFilePath = $dirName.'/'.rand().'.pdf';
     
     file_put_contents($newFilePath, $file);
?>