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
CodePudding user response:
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);
?>
CodePudding user response:
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);
?>