PHP Creating Zips Without Path to Files Inside the Zip

php creating zips without path to files inside the zip

The problem here is that $zip->addFile is being passed the same two parameters.

According to the documentation:

bool ZipArchive::addFile ( string $filename [, string $localname ] )

filename
The path to the file to add.

localname
local name inside ZIP archive.

This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);

Php zip files without directory tree

The second argument to the addFile() method is the name of the file inside the zip. You're doing this:

$zip->addFile($file, $file);

So you're getting something like:

$zip->addFile('/path/to/some/file', '/path/to/some/file');

Which just copies the path from the source into the zip. If you want all the files to be in one dir in the zip, remove the path from the file name:

$zip->addFile($file, basename($file));

This will give you:

$zip->addFile('/path/to/some/file', 'file');

Note this won't work too well if you have files with the same name in different directories.

how to create and download zip file without path directory

Your Code look's is Fine.
Just Replace Your Foreach Loop .

foreach ($files as $file)
{
$file = str_replace('\\', '/', $file);
if(filetype($file) == 'file') {
$zip->addFile( $file, pathinfo( $file, PATHINFO_BASENAME ) );
}
}

it should be work
Thanks

Creating a zip file in a specific path without any subfolders

Your code above doesn't work because you don't show the contents of $fileNames but I am assuming it is something like:

array('assets/uploads/pdfFiles/file1.txt','assets/uploads/pdfFiles/file2.txt',...)

When you add to the zip do this:

foreach ($fileNames as $fullpath) {
// pull just the file name from the path
$filename = substr($fullpath, strrpos($fullpath, '/')+1);
$zip->addFile($fullpath,$filename);
}

create zip file inside a folder but without folder inside that zip

@hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening

Then your code will not work at all as the path to $File is wrong. Use this:

$File = "images/files.txt";

$zip = new ZipArchive();
$filename = "./images/files.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}

// use the second parameter of addFile(). It will give the file
// a new name inside the archive. basename() returns the filename
// from a given path
$zip->addFile("$File", basename($File));

if(!$zip->close()) {
die('failed to create archive');
}

How to ZIP an entire folder in PHP, even the empty ones?

// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);

foreach ($files as $name => $file)
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);

if (!$file->isDir())
{
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}

// Zip archive will be created only after closing object
$zip->close();

Hi. I modified the previous answer and now I get the zip file including the empty folders. I hope this helps.



Related Topics



Leave a reply



Submit