Php zip get all files

ZipArchive::getNameIndex

Если флаг установлен в ZipArchive::FL_UNCHANGED , возвращается оригинальное неизмененное имя.

Возвращаемые значения

Возвращает имя при успехе или false в случае возникновения ошибки.

Примеры

Пример #1 Пример использования ZipArchive::getnameindex()

if ( $zip -> open ( ‘test.zip’ ) == TRUE ) for ( $i = 0 ; $i < $zip ->numFiles ; $i ++) $filename = $zip -> getNameIndex ( $i );
// .
>
>
?>

User Contributed Notes 1 note

I couldn’t find any how-to example for getting the filenames, so I made an easy one.

Here’s an example how to list all filenames from a zip-archive:

$zip = new ZipArchive ;
if ( $zip -> open ( ‘items.zip’ ))
<
for( $i = 0 ; $i < $zip ->numFiles ; $i ++)
<
echo ‘Filename: ‘ . $zip -> getNameIndex ( $i ) . ‘
‘ ;
>
>
else
<
echo ‘Error reading zip-archive!’ ;
>
?>

Hope it helps.

  • ZipArchive
    • addEmptyDir
    • addFile
    • addFromString
    • addGlob
    • addPattern
    • clearError
    • close
    • count
    • deleteIndex
    • deleteName
    • extractTo
    • getArchiveComment
    • getArchiveFlag
    • getCommentIndex
    • getCommentName
    • getExternalAttributesIndex
    • getExternalAttributesName
    • getFromIndex
    • getFromName
    • getNameIndex
    • getStatusString
    • getStream
    • getStreamIndex
    • getStreamName
    • isCompressionMethodSupported
    • isEncryptionMethodSupported
    • locateName
    • open
    • registerCancelCallback
    • registerProgressCallback
    • renameIndex
    • renameName
    • replaceFile
    • setArchiveComment
    • setArchiveFlag
    • setCommentIndex
    • setCommentName
    • setCompressionIndex
    • setCompressionName
    • setEncryptionIndex
    • setEncryptionName
    • setExternalAttributesIndex
    • setExternalAttributesName
    • setMtimeIndex
    • setMtimeName
    • setPassword
    • statIndex
    • statName
    • unchangeAll
    • unchangeArchive
    • unchangeIndex
    • unchangeName

    Источник

    ZIP all files in directory and download generated .zip [duplicate]

    How to gather all the files from «images/» folder, except «generate_zip.php», and make it a downloadable .zip? In this case the «images/» folder always have a different image. Is that possible?

    6 Answers 6

    ======= Working solution !======

    new GoodZipArchive('path/to/input/folder', 'path/to/output_zip_file.zip') ; 

    This is a good answer, but it’s not based on the question that OP asked, in which he specified that he only cared about the single directories image files. Just thought that should be clarified as anyone who is trying to do exactly what OP asked would find this code does not do exactly that.

    this will ensure a file with .php extension will not be added:

     foreach ($files as $file) < if(!strstr($file,'.php')) $zip->addFile($file); > 

    edit: here’s the full code rewritten:

    open($zipname, ZipArchive::CREATE); if ($handle = opendir('.')) < while (false !== ($entry = readdir($handle))) < if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) < $zip->addFile($entry); > > closedir($handle); > $zip->close(); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename='adcs.zip'"); header('Content-Length: ' . filesize($zipname)); header("Location: adcs.zip"); ?> 

    php.net/manual/en/function.readdir.php has what you need in that regards, example #2 is probably easiest. you could extend if ($entry != «.» && $entry != «..») to if ($entry != «.» && $entry != «..» && !strstr($entry,’.php’)) and do the zip add in that loop too instead of my above example.

    change $files = array($listfiles); to $files = array(); then in the while loop do array_push($listfiles,$entry);

    Since you just need specific files from a directory to create ZipArchive you can use glob() function to do this.

    open($download, ZipArchive::CREATE); foreach (glob("images/*.png") as $file) < /* Add appropriate path to read content of zip */ $zip->addFile($file); > $zip->close(); header('Content-Type: application/zip'); header("Content-Disposition: attachment; filename = $download"); header('Content-Length: ' . filesize($download)); header("Location: $download"); ?> 

    Don’t use glob() if you try to list files in a directory where very much files are stored (more than 100.000). You get an «Allowed memory size of XYZ bytes exhausted . » error.

    Источник

    Using ziparchive or other php script to allow user to download all files from a folder in zip

    So id like to offer my users the option to download all files from specific folders/directories from my site (lets call it mass download). What id like to do is when the user clicks a link/button the script will create a temporary zip file of all the files in the specific folder and the user will be able to download it. (I will need different instances of the link/button on different pages to download files from other folders that I choose if that makes sense). The zip file will be deleted after sometime. Say after download is complete or something. I think ziparchive can do this but I dont know where to start or how to implement it. Its a joomla site and I can’t find any extensions that can do this. I dont know the first thing about php so I am hoping some one will be willing to help me get this working if thats possible. Thanks

    1 Answer 1

    If your server allows execution of shell commands from PHP, and zip is installed, you could generate a zip on the fly with passthru( «zip — directory» ) . The — says to write to stdout, which saves you from having to deal with temporary file cleanup.

    Here’s an outline of such a script:

    However you implement get_my_directory() , make sure that it isn’t possible for anyone to specify any path on your server!

    Also, do not generate any output (no echo / print or warnings), because then either the headers won’t be set, or the zip binary data will be corrupt.

    Other than that, there are code samples and documentation on PHP’s ZipArchive page.

    (@ OP: I’m not really sure what you’re doing implementing PHP solutions if you don’t know any PHP. But, let’s assume that you want to learn. )

    Lets say that you have 3 public directories you would like to offer for download, and that anyone can download them. You would implement as follows:

    function get_my_directory() < // list of the directories you want anyone to be able to download. // These are key-value pairs, so we can use the key in our URLs // without revealing the real directories. $my_directories = array( 'dir1' =>'path/to/dir1/', 'dir2' => 'path/to/dir2/', 'dir3' => 'path/to/dir3/' ); // check if the 'directory' HTTP GET parameter is given: if ( ! isset( $_GET['directory'] ) ) return null; // it's not set: return nothing else $dir = $_GET['directory']; // it's set: save it so we don't have // to type $_GET['directory'] all the time. // validate the directory: only pre-approved directories can be downloaded if ( ! in_array( $dir, array_keys( $my_directories ) ) ) return null; // we don't know about this directory else return $my_directories[ $dir ]; // the directory: is 'safe'. > 

    And yes, you paste the first and second code sample in one .php file (be sure to replace the first get_my_directory function with the second one), somewhere on your server where it is accessible.

    If you call the file ‘download-archive.php’, and place it in the DocumentRoot, you would access it as http://your-site/download-archive.php?directory=dir1 etc.

    • PHP’s tutorial
    • functions in general
    • function header
    • function passthru
    • function die
    • function in_array
    • function array_values

    Here’s a complete script using ZipArchive. It only adds files in the directory; no subdirectories.

     ); header( "Content-Type: application/zip" ); header( "Content-Disposition: attachment; filename=\"$zipfile\"" ); readfile( $zipfile ); function make_zip( $dir ) < $zip = new ZipArchive(); $zipname = 'tmp_'.basename( $dir ).'.zip'; // construct filename if ($zip->open($zipname, ZIPARCHIVE::CREATE) !== true) die("Could not create archive"); // open directory and add files in the directory if ( !( $handle = opendir( $dir ) ) ) die("Could not open directory"); $dir = rtrim( $dir, '/' ); // strip trailing / while ($filename = readdir($handle)) if ( is_file( $f = "$dir/$filename" ) ) if ( ! $zip->addFile( $f, $filename ) ) die("Error adding file $f to zip as $filename"); closedir($handle); $zip->close(); return $zipname; > /** * @return false/null or the directory to zip. */ function get_my_directory() < // list of the directories you want anyone to be able to download. // These are key-value pairs, so we can use the key in our URLs // without revealing the real directories. $my_directories = array( 'dir1' =>'path/to/dir1/', 'dir2' => 'path/to/dir2/', 'dir3' => 'path/to/dir3/' ); // check if the 'directory' HTTP GET parameter is given: if ( ! isset( $_GET['directory'] ) ) return null; // it's not set: return nothing else $dir = $_GET['directory']; // it's set: save it so we don't have // to type $_GET['directory'] all the time. // validate the directory: only pre-approved directories can be downloaded if ( ! in_array( $dir, array_keys( $my_directories ) ) ) return null; // we don't know about this directory else return $my_directories[ $dir ]; // the directory: is 'safe'. > 

    Источник

    Читайте также:  Ispmanager версия php cron
Оцените статью