J'essaie d'enregistrer fichiers de n dossier à n autre. Dossier Zip placé dans un répertoire différent. Et j'ai écrit les codes suivants:
archive.php
<?php
$Zip = new ZipArchive();
$Zip->open('example.Zip', ZipArchive::CREATE);
$srcDir = "/home/sam/uploads/";
$files= scandir($srcDir);
//var_dump($files);
unset($files[0],$files[1]);
foreach ($files as $file) {
$Zip->addFile("{$file}");
}
$Zip->close();
?>
Mais malheureusement, je ne peux pas créer le dossier .Zip. Y a-t-il une étape que j'ai ratée?
$Zip = new ZipArchive();
$DelFilePath="first.Zip";
if(file_exists($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath)) {
unlink ($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath);
}
if ($Zip->open($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
die ("Could not open archive");
}
$Zip->addFile("file_path","file_name");
// close and save archive
$Zip->close();
Ici TEST est le nom de votre dossier de projet.
Vous pouvez définir le chemin d'accès comme vous le souhaitez.
Oui, j'ai résolu mon problème.
Ici, je viens de remplacer le code
$Zip->addFile("{$file}");
avec le code
$Zip->addFromString(basename($file), file_get_contents($file));
et faire mon travail. :)
Essayez ce code:
<?php
$Zip = new ZipArchive;
if ($Zip->open(getcwd() . '/test.Zip', ZipArchive::CREATE) === TRUE) {
$Zip->addFile(getcwd() . '/file.txt', 'newname.txt');
$Zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Veuillez voir cet exemple ci-dessous:
<?php
$error = ""; //error holder
if(isset($_POST['createpdf'])){
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('Zip')){ // Checking Zip extension is available
if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected
$Zip = new ZipArchive(); // Load Zip library
$Zip_name = time().".Zip"; // Zip name
if($Zip->open($Zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening Zip file to load files
$error .= "* Sorry Zip creation failed at this time<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/>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip</title>
</head>
<body>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
<tr>
<td width="33" align="center">*</td>
<td width="117" align="center">File Type</td>
<td width="382">File Name</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>a.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>b.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td>
<td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
<td>c.docx</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td>
<td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
<td>d.pdf</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as Zip" />
<input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>