簡體   English   中英

php如何從url下載文件並保存到服務器

[英]php how download file from url and save it on server

我的鏈接 (URL) 不同!!! 並且不適用於通常的方法我認為因為網站加載 js 或 aspx 你可以在你的瀏覽器中測試我的鏈接(URL)並看到下載開始但不能在 php 中工作

我已經測試了所有方法(fetch、curl、file、get、put),但它不起作用。 我這里有一個類似的 URL:'http://www.tsetmc.com/tsev2/data/ClientTypeAll.aspx?h=0&r=0'

我可以在瀏覽器中打開它並下載一個 csv 文件我需要在 php 中執行此操作並將 csv 文件保存在服務器上

到目前為止,這是我嘗試過的:

<?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);

?>

我不要內容!!! 如果你在瀏覽器中測試我的鏈接,我想要一個 csv 文件從你的電腦開始下載

我使用遠程 PDF 文件運行此代碼。

<?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);

?>

請按照以下步驟操作。

  • 獲取文件表格 URL。
  • 設置目錄並檢查文件退出條件並更新目錄訪問權限。
  • 設置新的文件路徑、名稱和目錄。
  • 保存文件。

請檢查以下對我有用的示例。

<?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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM