Using zipArchive addFile() will not add image to zip

Asked
Active3 hr before
Viewed126 times

9 Answers

imageusing
90%

The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:,I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist. ,I had similar kind of issue and it was related with the file that I was going to add to the zip archive.,Before adding file to zip archive, it's always better to check if the file exists.

The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:

// $zip->addFile($file);
$content = file_get_contents($file);
$zip - > addFromString(pathinfo($file, PATHINFO_BASENAME), $content);
load more v
88%

ZipArchive::addFile — Adds a file to a ZIP archive from the given path, Adds a file to a ZIP archive from a given path. ,ZipArchive::replaceFile() - Replace file in ZIP archive with a given path, This example opens a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt.

load more v
72%

Board index Web Development PHP - MySQL , Board index , Quick links Unanswered topics Active topics Search ,Announcements & Support CoursesWeb Announcements will be made here. You can post topics about any issue related to this website and forum. 10

< ? php
$today = date("F j, Y, g:i a");

$files = 'http://145.53.93.209/Molendijk/add/load/pictures/'.$users["post_image"].
'';

$zip = new ZipArchive;
if ($zip - > open(''.$users["post_user_name"].
      '/Backup/'.$users["title"].
      '.zip', ZipArchive::CREATE) == TRUE) {
   // Add files to the zip file
   $zip - > addFile(''.$users["post_user_name"].
      '/Backup/'.$users["title"].
      '');

   $zip - > addFile(''.$users["post_user_name"].
      '/Backup/'.$users["title"].
      '/$files');

   // Add File3.txt file to zip and rename it to File5.txt

   // Add a file newFile.txt file to zip by adding specified text in newFile.txt
   $zip - > addFromString('@Molendijk-'.$users["title"].
         '.txt', 'Welcome to @Molendijk I just extracted your '.$users["title"].
         ' 
         on '.date("F j, Y, g:i a").'
         your located with ip: '.$_SERVER["REMOTE_ADDR"].'

         '.$users["post_text"].'

         ');

         // All files are added, so close the zip file.
         $zip - > close();
      } ?
      >
load more v
65%

If the reading value is the file then add it to zip object using addFile() method.,PHP provides ZipArchive Class which allows us to create a Zip file. This class makes file creation easier.,You can also use ZipArchive Class to unzip the Zip file.,Home » PHP » How to create and download a Zip file with PHP

Completed Code

<div class='container'>
   <h2>Create and Download Zip file using PHP</h2>
   <form method='post' action=''>
      <input type='submit' name='create' value='Create Zip' />&nbsp;
      <input type='submit' name='download' value='Download' />
   </form>
</div>
load more v
75%

To append more files into an existing zip archive, we simply open it again and use the addFile() function; To add the file into a folder within the zip archive, we only have to specify it accordingly – addFile(TARGET.FILE, FOLDER-IN-ZIP/SAVE.FILE). ,PHP comes with a very handy glob() function to filter out files/folders in the system. There is also a addGlob() function that we can use in the zip archive – To add an entire folder or filter out certain file extensions only. ,All right, let us now get into the examples of the various ways to add files and folders into a zip archive in PHP. ,We can add files and folders to a zip file in PHP using the ZipArchive extension:

< ? php
// (A) NEW ZIP ARCHIVE OBJECT
$zip = new ZipArchive();
$zipfile = "demoA.zip";

// (B) OPEN/CREATE ZIP FILE
if ($zip - > open($zipfile, ZipArchive::CREATE) === true) {
   // (B1) ADD FILE
   echo $zip - > addFile("readme.txt", "filename-in-zip.txt") ?
      "File added to zip archive" : "Error adding file to zip archive";

   // (B2) ADD FOLDER
   echo $zip - > addEmptyDir("folder") ?
      "Folder added to zip archive" : "Error adding folder to zip archive";

   // (B3) CLOSE ZIP
   echo $zip - > close() ?
      "Zip archive closed" : "Error closing zip archive";
}

// (C) FAILED TO OPEN/CREATE ZIP FILE
else {
   echo "Failed to open/create $zipfile";
}
load more v
40%

The file added by this call to the ZipFile is not written to the zip file archive until the application calls Save() on the ZipFile. , This method will throw an exception if an entry with the same name already exists in the ZipFile. , AddFile Method (fileName, directoryPathInArchive) , AddFiles Method (fileNames, directoryPathInArchive)

public ZipEntry AddFile(
   string fileName,
   string directoryPathInArchive
)
load more v
22%

How to zip files using the Syncfusion.Compression.Zip namespace?,You can use CompressionLevel to reduce the size of the file.,The following code snippet illustrate how to add the file from the local drive.,How to un-protect the zip files using Syncfusion.Compression.Base?

using Syncfusion.Compression.Zip;

ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();

zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;

//Add the file you want to zip.

zipArchive.AddFile("SampleFile.cs");

//Zip file name and location.

zipArchive.Save("SyncfusionCompressFileSample.zip");

zipArchive.Close();
load more v
60%

The following examples show some of the operations you can perform with compressed files. These examples require the following NuGet packages to be added to your project:,To run the sample, create a start folder in your program folder and populate it with files to zip.,The System.IO.Compression namespace contains the following types for compressing and decompressing files and streams. You can also use these types to read and modify the contents of a compressed file.,The following example shows how to create and extract a compressed .zip file by using the ZipFile class. The example compresses the contents of a folder into a new .zip file, and then extracts the zip to a new folder.

To run the sample, create a start folder in your program folder and populate it with files to zip.

using System;
using System.IO.Compression;

class Program {
   static void Main(string[] args) {
      string startPath = @ ".\start";
      string zipPath = @ ".\result.zip";
      string extractPath = @ ".\extract";

      ZipFile.CreateFromDirectory(startPath, zipPath);

      ZipFile.ExtractToDirectory(zipPath, extractPath);
   }
}
load more v
48%

We looped through our array of file paths and added them to the archive by using the ZipArchive::addFile function.,The ZipArchive::close function is then used to close off the archive and save the ZIP file.,We created a PHP array and added the full path to each file that we want to add to our archive.,We specified the full path to where we want to save our Zip Archive. This path should also include the filename and the .zip file extension.

Take a look at the following PHP example, which uses the ZipArchive class:

//Create an object from the ZipArchive class.
$zipArchive = new ZipArchive();

//The full path to where we want to save the zip file.
$zipFilePath = '/home/data/example.zip';

//Call the open function.
$status = $zipArchive - > open($zipFilePath, ZipArchive::CREATE);

//An array of files that we want to add to our zip archive.
//You should list the full path to each file.
$filesToAdd = array(
   '/home/data/image.png',
   '/home/data/test.jpg'
);

//Add our files to the archive by using the addFile function.
foreach($filesToAdd as $fileToAdd) {
   //Add the file in question using the addFile function.
   $zipArchive - > addFile($fileToAdd);
}

//Finally, close the active archive.
$zipArchive - > close();

//Get the basename of the zip file.
$zipBaseName = basename($zipFilePath);

//Set the Content-Type, Content-Disposition and Content-Length headers.
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$zipBaseName");
header("Content-Length: ".filesize($zipFilePath));

//Read the file data and exit the script.
readfile($zipFilePath);
exit;

Other "image-using" queries related to "Using zipArchive addFile() will not add image to zip"