How can PHP be utilized to download a file from a server, unzip it, create a new directory in the webroot, and copy files into it?
To download a file from a server, unzip it, create a new directory in the webroot, and copy files into it using PHP, you can use the `file_get_contents()` function to download the file, `ZipArchive` class to unzip it, `mkdir()` function to create a new directory, and `copy()` function to copy files into it.
// Download the file
$fileUrl = 'http://example.com/file.zip';
$fileContent = file_get_contents($fileUrl);
// Unzip the file
$zip = new ZipArchive;
$zip->open('file.zip');
$zip->extractTo('temp/');
$zip->close();
// Create a new directory in the webroot
$newDir = 'new_directory';
mkdir($_SERVER['DOCUMENT_ROOT'] . '/' . $newDir);
// Copy files into the new directory
$files = glob('temp/*');
foreach ($files as $file) {
copy($file, $_SERVER['DOCUMENT_ROOT'] . '/' . $newDir . '/' . basename($file));
}
Keywords
Related Questions
- What is the best practice for maintaining session consistency across multiple domains in PHP?
- How can the file_get_contents function be utilized as an alternative to the undefined gethtml() function in PHP?
- What are the recommended PHP coding practices for beginners to avoid common pitfalls like undefined indexes in form processing?