How can PHP be used to automate the process of downloading and saving files from a specific URL on a web server?
To automate the process of downloading and saving files from a specific URL using PHP, you can utilize the `file_get_contents()` function to retrieve the file contents and then use `file_put_contents()` to save the contents to a local file on the server.
$url = 'https://example.com/file-to-download.zip';
$localFilePath = 'downloaded-file.zip';
$fileContents = file_get_contents($url);
if ($fileContents !== false) {
file_put_contents($localFilePath, $fileContents);
echo "File downloaded and saved successfully.";
} else {
echo "Failed to download file.";
}
Keywords
Related Questions
- What potential security risks should be considered when allowing users to upload images on a website using PHP?
- What are best practices for installing PHP applications like WoltLab Burning Board to ensure successful database connection and operation?
- What are some best practices for structuring and organizing PHP code to handle time-based tasks efficiently and effectively?