Pinning files via shell_exec in PHP: path problem
I am working on a web backup service part of which allows the user to download a zip file containing the files they have selected from a list of backups.
The zipping process is performed using the shell_exec command. To do this, I gave apache to users without sudo password privileges for the zip command.
My problem is that I cannot do "sudo cd / path / to / user / files" to the folder containing user backups, I need to give the full absolute path to the files to be zipped and then go to the zip file which I don't need.
This is what I mean:
User "test1" has backups of his files, which are stored as follows: / home / backups / test 1 / my pics / pic1.jpg
/ home / backups / test 1 / my pics / pic2.jpg
/ home / backups / test 1 / my pics / pic3.jpg
/ home / backups / test 1 / hello.txt
/ home / backups / test 1 / music / band / song.mp3
When these files are zipped, they keep full paths, but I just want them to be displayed:
my photos / pic 1.jpg
my photos / pic 2.jpg
my photos / pic 3.jpg
hello.txt
music / band / Song.mp3
Is there a way to tell zip to truncate paths, or to provide a custom path for each file?
Or, I can change the working directory to the user's root folder, so I can just provide relative file paths.
Thanks for reading, I hope you got this far and it all makes sense!
source to share
I need to ask a bit why you are making a potentially dangerous system wrapper call when there are a number of good PHP scrolling classes. the tutorial here shows how easy it is to create a class that will output the zip file to the browser. There are also a number of classes at phpclasses.org, this seems to be the best one .
If you need to do this with a system call, my suggestions are:
You can use symbolic links to truncate the file path .
Can't you increase the permissions on the zip executable so that it can use it without using sudo?
source to share
A better idea might be to create a shell script and give the sudo webserver access to that shell script. It might be more secure as well
#!/bin/bash
#
# Zip up files for a given user
#
# usage: backupToZipFile.sh <username>
cd home/backups/$1
tar -czf /tmp/$1.tgz .
Also do you think you are using sudo as the user trying to zip files and not as root? It would also be more secure.
source to share
This seems to be a simple option, but according to man
it, it simply isn't. You could, of course, symbolically link to the stuff you want to archive in the place where you create the zip file (I assume you can actually connect to that directory). With the correct parameters zip
, it will instead not archive the symlinks themselves, except for the symlinks using the symlink names.
eg. create symlinks / tmp / $ PID / my pics / pic1.jpg etc. and then write everything to / tmp / $ PID.
source to share