以下是我的要求,但我无法理解如何使用PHP进行此操作。
我探索了不同的选项,比如使用SSH2,curl,file_get_contents。但我无法确定这一切是如何运作的。
答案 0 :(得分:1)
试试此代码
create upload.php
<?php
$error = ""; //error holder
if(isset($_POST['compress'])){
$post = $_POST;
$file_folder = "files/"; //Folder where files are exists
if(extension_loaded('zip'))
{ // Checking ZIP extension is available
if(isset($post['files']) && count($post['files']) > 0){
$zip = new ZipArchive(); // create zip class object
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Open zip object
$error .= "* Zip file could not be create<br/>";
}
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}else
$error .= "* Please select file to zip <br/>";
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>
Create index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip/RAR</title>
</head>
<body>
<center><h1>Create a Compress file with PHP</h1></center>
<form name="zips" method="post">
<table>
<?php
if($error!='')
{
?>
<center><h5 style="color:red;"><?php echo $error; ?></h5></center>
<?php
}
?>
<tr>
<td><input type="checkbox" name="files[]" value="car.jpg"></td><td><img src='car.jpg' width='50'></td><td>Car.jpg</td></tr>
<tr><td><input type="checkbox" name="files[]" value="plane.jpg"></td><td><img src='plane.jpg' width='50'></td><td>Plane.jpg</td></tr>
<tr><td><input type="checkbox" name="files[]" value="bike.jpg"></td><td><img src='bike.jpg' width='50'></td><td>Bike.jpg</td></tr>
<tr><td><input type="submit" name="compress" value="compress"></td><td></td><td></td></tr>
</table>
</form>
</body>
</html>
答案 1 :(得分:0)