Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

TutsPlus.com:
How to Zip and Unzip Files in PHP
Oct 24, 2018 @ 18:35:56

The TutsPlus.com site has a tutorial posted showing how to make use of the ZIP functionality in PHP to handle the compression and decompression of ZIP archives programatically.

Compressing files when transferring them over the internet has a lot of advantages. In most cases, the combined total size of all the files in the compressed format comes down by a nice margin. This means that you will save some of your bandwidth, and users will also get faster download speeds.

[...] One factor that can discourage you from compressing files or make the process very tiresome is the fact that you might be doing it manually. Luckily, PHP comes with a lot of extensions that deal specifically with file compression and extraction. You can use the functions available in these extensions to automatically compress files in PHP.

This tutorial will teach you how to zip and unzip (compress and extract) files to and from a zip archive in PHP. You will also learn how to delete or rename files in an archive without extracting them first.

The tutorial starts with the compression of files making use of the ZipArchive class to create a new instance, add files to compress and close it out to write the resulting archive. Next it shows the reverse, using the same class to create an instance and extraction path. There's also an example of using a loop for even more control of which files are included in the archive.

tagged: tutorial zip unzip introduction ziparchive archive compress

Link: https://code.tutsplus.com/tutorials/file-compression-and-extraction-in-php--cms-31977

Oscar Merida:
Creating ZIP files and streaming the response with Silex
Jan 19, 2018 @ 17:28:44

On his site Oscar Merida has shared a post showing how to create a ZIP file and stream the response in a Silex-based application using the DOMPDF library and PHP's ZipArchive functionality.

Creating a zip file is easy and Rob Allen has you covered on that front to get you started. I needed to do this to create a zip file of PDF files generated by Dompdf. It turns out, you can add files to a ZipArchive object without actually creating the file if you can get the contents as a string.

The post includes the code to create the DOMPDF instance, load the HTML and render the result. From there the ZipArchive takes over and creates the archive from the PDF result. Finally the code takes the ZIP archive as input and pushes it out with a custom header to tell the browser to download it and streams back the raw ZIP contents.

tagged: tutorial silex dompdf pdf zip archive silex stream download

Link: http://oscarm.org/2018/01/stream-a-zip-file-via-silex/

TutsPlus.com:
Dynamic Page Templates in WordPress, Part 3
Jun 19, 2017 @ 15:45:04

The TutsPlus.com site has posted the third part of their "Dynamic Page Templates in WordPress" tutorial series today. In this latest article author David Gwyer finishes off the series using all that they've shared from part one and part two to create two examples.

In the first two parts of this tutorial series, we covered what dynamic page templates were and why they were needed. We also looked at the code required to implement them.

In this third and final tutorial in the series, I'll be creating two examples of fully working dynamic page templates you can use in your own projects. These were specifically chosen to be easily extendable to suit your own needs, and are intended as inspiration for any other type of dynamic page templates you can think of.

He then walks you through the creation of the two page templates: a Simple Contact Form and a Blog Post Archive. The first allows you to dynamically control the form elements for a UI interface (rather than code) and the second uses dynamic data to display the list of previous blog posts. The tutorial then finishes with a look at how, since WordPress 4.7, you can use dynamic page templates with any kind of post, not just pages.

tagged: wordpress series part3 dynamic page template blog archive simple form tutorial

Link: https://code.tutsplus.com/tutorials/dynamic-page-templates-in-wordpress-part-3--cms-28514

Weebly Engineering Blog:
Streamed File Zipping and Downloading in PHP
Feb 06, 2017 @ 17:24:01

The Weebly Engineering blog has a new tutorial posted about streamed file zipping and downloading in a PHP based application. This allows the download to start even while the file is being compressed into a zip archive on the backend.

Did you notice that on Dropbox.com, you can select a folder and start downloading it while it’s being zipped? This on-the-fly zipping feature comes handy for both the user and the server?—?as a user you don’t have to wait until the files are zipped on the back end before the downloading starts, and it saves the server from creating a temporary zip file and deleting it afterwards.

[...] To implement this feature with PHP, we handle the zipping and streaming work on the back end using ZipStream. Assuming your files are on the server and can be loaded with file_get_contents given a path.

They include some of the details of the HTTP request format for the stream (as an attachment type) and how you can use the ZipStream package to quickly implement it into your code. They include a simple example of implementing it in a form, allowing the user to easily integrate it and work around some of the browser based security controls.

tagged: stream zip archive download tutorial zipstream tutorial

Link: https://medium.com/weebly-engineering/streamed-file-zipping-and-downloading-in-php-1fb59b0a5b79#.jop61kzc6

SitePoint PHP Blog:
Sculpin Extended: Customizing Your Static Site Blog
Aug 19, 2016 @ 17:22:56

The SitePoint PHP blog has a new tutorial posted helping those Sculpin users out there get the most from their site with some helpful customization tips. Sculpin is a PHP-based static site generation tool that converts Mardon files and Twig templates into HTML documents ready for use.

If you’re a PHP developer and currently running a blog with a static site generator such as Octopress or Jekyll, wouldn’t it be great if you could use your primary language for it? Yes, it’s healthy for us developers to use more than one language, but let’s be honest – we often want to add some functionality to our blogs, but it’s difficult to accomplish in unfamiliar syntax. In this article, we’ll set up Sculpin, a static site generator for PHP. Just like any other static site generator, it uses markdown files and HTML templates to generate your blog, so the transition should be easy.

The tutorial starts by helping you get Sculpin installed (as a phar executable) and move it to where it's globally accessible. With that installed the article then helps you make a simple blog, customize some of the basic settings and start in on a new blog post. With that in place it then gets into the customization, adding in:

  • syntax highlighting
  • Disqus commenting
  • blog archive links

The post finishes up showing you how to deploy the resulting blog into a GitHub pages repository and pushing them out for public consumption.

tagged: sculpin extended tutorial static site github pages syntaxhighligh disquis archive

Link: https://www.sitepoint.com/sculpin-extended-customizing-your-static-site-blog/

Christian Weiske:
Running a phar without .phar extension
Dec 18, 2015 @ 17:18:13

Christian Weiske has a quick tip posted to his site for those using phar archives in PHP and how to remove the .phar extension and still have them execute correctly.

I'm shipping a new side project as .phar file, php-sqllint.phar. The phar stub has a shebang line so that I can call it without explicitly running PHP. [...] When making the file executable with chmod +x, you can simply execute it.

He wanted to remove the extension but was greeted with an error message when he tried to execute the result. He stumbled on the solution in this PHPUnit bug and corrected the problem with a call to the Phar::mapPhar method inside the phar archive file.

tagged: phar archive extension remove mapphar tutorial

Link: http://cweiske.de/tagebuch/phar-renaming-no-ext.htm

Matthew Weier O'Phinney:
Secure PHAR Automation
Dec 15, 2015 @ 18:32:54

There's always been an issue with the creation of Phar packages in PHP and the security around them. There's been recommendations about creating signatures and only using secure connections for updates and rollbacks. Unfortunately there isn't an overly easy way to handle this yet. However, Matthew Weier O'Phinney has written up a post showing his workflow for doing these kinds of things, making use of the Box project to help with some of the more detailed parts.

For a variety of reasons, I've been working on a utility that is best distributed via PHAR file. As has been noted by others (archive.is link, due to lack of availability of original site), PHAR distribution, while useful, is not without security concerns, and I decided to investigate how to securely create, distribute, and update PHAR utilities as part of this exercise.

This is an account of my journey, as well as concrete steps you can take to secure your own PHAR downloads.

He starts by outlining the "roadmap" of the features he wants to include and the steps to take to create this more secure phar archive. It includes the use of both current, local tools and services (like Box and GitHub pages). He then walks through the steps in the full process:

  • Create an OpenSSL Key
  • Use Box to create the PHAR
  • Generate a version file
  • Create the gh-pages branch
  • Write self-update/rollback commands
  • Enable Travis-CI for the repository
  • Create an SSH deploy key
  • Archive and encrypt the secrets
  • Write a deployment script
  • Add the script to travis

While this seems like a lot of steps to just get a more secure phar set up, Matthew has done the hard work for you here and includes all of the commands, configuration examples and steps you'll need to take to fully set the process up. If all goes well, his example in his last "push and watch it work" section will go off without a hitch.

tagged: phar archive security signed https update rollback travisci tutorial

Link: https://mwop.net/blog/2015-12-14-secure-phar-automation.html

ServerGrove Blog:
Introduction to the PHAR format
Jul 31, 2015 @ 17:15:44

The ServerGrove blog has posted an introduction to the PHAR format, a built-in method to use PHP and create self-contained functional scripts as a single *.phar file making it much easier to transport.

In the last years there is a trend in the PHP community to release tools, especially command line utilities, as PHAR files, so you can package an entire PHP application into a single file for convenience. But, how PHAR files work? In this post we will try to explain it.

They cover a few of the basic topics first: what is a PHAR file and a few examples of them being provided by major PHP projects. They then get into the creation of an archive, showing how to make a super simple PHAR "Hello World" archive, created with just a bit of PHP. They then get into the structure behind the archive and get into detail on each section (stub, manifest, file contents and signature).

tagged: phar format introduction archive package

Link: http://blog.servergrove.com/2015/07/30/introduction-phar-format/

SitePoint PHP Blog:
Boxing up your Apps as Phars Quickly and Easily with Box
Jun 16, 2015 @ 13:44:27

The SitePoint PHP blog has a new tutorial posted showing you how to easily package up your application with Box to make phar files without the extra hassle of building them yourself.

In this tutorial, we’ll use Box to package a PHP application into a Phar, in order to make it easily distributable and globally installable via Composer.

For his example he uses the PHP portion of the FolderBuilder project and makes a command-line executable that can return the information for a directory as JSON data. He starts by installing the "box" executable command on a local VM and defines the simple configuration file, a "box.json" with some basic settings. He then clones the FolderBuilder project, updates the configuration for the correct locations and files and executes the "build" command. The result is a phar file that contains the PHP script functionality. He also updates the configuration to make the result executable with a "chmod" setting, removing the need to call it with the PHP command line version. He ends the post showing how to test it out, taking the results and dropping them into FolderBuilder to make sure they're 100% correct.

tagged: tutorial build phar archive easy box commandline tool

Link: http://www.sitepoint.com/boxing-apps-phars-quickly-easily-box/

SitePoint PHP Blog:
Strategic Archive Extraction with Distill
Oct 27, 2014 @ 17:09:54

In this new tutorial from the SitePoint PHP blog about using the Distill tool to extract information and files from remote archives.

Perhaps you are building an application which depends on archives; for example, you constantly have to download archives and extract files from them. There are many libraries out there that can help you get files extracted from an archive, and a new player in town capable of doing this job is Distill. With Distill, you can easily extract an archive into a specified directory. You can also give multiple archives to Distill and let it pick the most optimal one, as per a strategy you define yourself.

He walks you through the setup of the tool (installed via Composer) and some of the basic usage. He creates a simple "Extractor" object setting the Distill object and an "extract" method that handles the actual functional part of the process. He also adds some configuration constants to the class for size checking, compression speed and random strategy types (Distill will pick the most optimal). He then makes a "chooser" method to pick the best one and calls the "extract" method to get the results.

tagged: tutorial extract archive distill library tool

Link: http://www.sitepoint.com/strategic-archive-extraction-distill/


Trending Topics: