To download a file to a specified directory, you can use PHP's built-in functions. copy()This function can copy a file from one location to another.
Here is a basic example code that downloads a file from a remote server and saves it to a specified local directory:
$remote_file_url = 'http://example.com/remote_file.txt';
$local_file_path = '/path/to/local/directory/local_file.txt';
// 使用 copy() 函数将文件从远程服务器下载到本地目录
if (copy($remote_file_url, $local_file_path)) {
echo "文件已成功下载到 $local_file_path.";
} else {
echo "下载文件失败。";
}In the above code, you need to $remote_file_url Replace with the URL address of the remote file. $local_file_path Replace with the full path and filename of the local directory.
If the download is successful, the system will output "File successfully downloaded to $local_file_path.". If the download fails, the system will output "Download failed."
Note: The PHP server must have sufficient permissions to write to the target directory; otherwise, the download may fail.