是的,file_get_contents
可以用來下載文件。當你想要從指定的 URL 下載文件并將其保存到本地時,可以使用 file_get_contents
函數(shù)結(jié)合 PHP 的 fopen
和 fwrite
函數(shù)。以下是一個簡單的示例:
<?php
// 要下載的文件的 URL
$url = 'https://example.com/path/to/your/file.txt';
// 本地保存文件的路徑
$localFilePath = 'downloaded_file.txt';
// 使用 file_get_contents 從 URL 下載文件并保存到本地
$content = file_get_contents($url);
if ($content === false) {
die('Error: Failed to download the file.');
}
// 將下載的內(nèi)容寫入本地文件
if (!file_put_contents($localFilePath, $content)) {
die('Error: Failed to save the downloaded file.');
}
echo 'File downloaded successfully and saved as ' . $localFilePath;
?>
這個示例將從給定的 URL 下載文件,并將其保存為名為 downloaded_file.txt
的本地文件。請確保將 $url
變量替換為要下載的實際文件的 URL。