I'm trying to create a script to read my files and add them to a gzip file. The code below works except that the paths are not written. So if the code below is ran as it is, the file within the gzip file will have the full contents of both index.php files in one file. I want to have it keep the directory structure like is seen in such gz files. Would someone please point out where my mistake is?
Gzip compresses (and can only compress) a single file. To create a package of files maintaining structure, you need to first create a tarball, then compress that. This is why you see the file extensions .tar.gz and .tgz for archived packages (gzipped tarball). The only things that are compressed directly by gzip are usually long text files, such as log files, etc.
Depending on what you are attempting to do, you may be better off writing a bash script to create your compressed archive. PHP has the exec() function to run command line operations, but using such methods is only recommended if you are well versed in the security concerns it raises.