I have designed an online editor with which a user can create files and folders on the server. Suppose a user creates the below files and folders.
/docs/abc.txt
/docs/def.txt
/docs/work/assignment.txt
I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click.
The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. I don't care even if the ZIP file is not compressed.
Note that I am using a shared server and do not have access to execute external commands.
ZipArchive class is what you need. Using this you can add directories and file to an archive.
This is a common requirement and has been solved. Try and check out this class: http://www.phpclasses.org/browse/package/2322.html
More tutorials at:
http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down
http://www.w3schools.com/php/php_ref_zip.asp
Assuming you have permission to execute an external command, you could use exec to run an external command-line capable ZIP program such as 7-Zip.
Related
If I place video file to for example
www.myweb.com/webroot/video.mp4
I am able to play it in VLC player in streaming option. I am using CakePHP 3 framework.
Where this file is placed in
/volume1/web/myweb/webroot/video.mp4.
But my question is how do I create link to file which is in Synology NAS, its link is:
'/volume1/video/Films/video.mp4'
To be accessible from a web?
I added to open_basedir in PHP in a directory :/volume1/video;
Instead of pointing to the actual file, have you tried reading the file using a PHP script?
For example, your site could have a link such as yoursite.com/video.mp4, but instead, you could have yoursite.com/getfile.php?f=video.mp4.
This getfile.php script can securely get the file from anywhere in your file server where your www user has access.
I personally use the readfile_chunked function documented here in the comments, as well as the appropriate headers needed to be sent before.
Note: Make sure you secure your getfile.php script to prevent users from reading any files on your drive. In my case, I force my script only to accept certain keys which correspond to actual files (ex: yoursite.com/getfile.php?filekey=Ab183Fca82, where Ab183Fca82 points to video.mp4)
I am creating a script that creates a subdomain on a whm server and then uploads a zip through ftp to it. That part is working fine and my newly created subdomain functions properly and the file I want gets transfered to it.
All I need is to unzip this file. One can easily do it using cpanel's built in file manager but I need this done in my own script. Is there some way to invoke cpanel's zip extraction functionality from my script?
Using PHP's ZipArchive is a problem because it's open() and extractTo() functions cannot take url as argument and the zip file I am talking about is not located in a place that I can access without a url. Here's why:
While I am logged in with cpanel_user_1 I created a subdomain that belongs to it's newly created user (let's say cpanel_user_2). I cannot just open the files in cpanel_user_2's public_html folder because that's the way cpanel works - I don't have access to another cpanel user's files. If ZipArhive could work with url I would just do:
$zip = new ZipArchive;
$zip->open('http://subdomain.domain.com/some_zip.zip')
$zip->extractTo('http://subdomain.domain.com/folder/some_zip.zip');
$zip->close();
But it doesn't... and I don't have access to the actual file path of the file.
I'm working on a php script packaging some files (a setup.exe and a licence.txt which is created direct in php for the user). We don't want that the user has to unzip it and start setup.exe.
On the computer we solved that by using selfextracting zips with:
zip is called setup.exe
zip has icon of the application
destination of extraction is temporary folder
extracting process is hidden
So if the user click on setup.exe he don't know that this setup.exe is a zip at the end.
But is that also possible on the server in the php script?
i tried the normal ziparchive object and pclzip and i can create the zip with these libraries. But i cannot change the parameters above...
Can anyone tell me if there's a solution for this?
Use PHP:exec to run an external application that compress and compile your exec file, with your stuff inside as you need.
PHP don't have built-in function to compress and compile and executable file.
Is it possible to create .php file and when I access it, I can also access all my other ftp files? Download them, edit them or upload other files. It would be very comfortable.
We've used this in the past:
File Thingie • PHP File Manager
It's nice and simple and is just a single php file.
Something like PHP File Manager?
I had good results with File Thingie myself.
Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.