1

我正在开发一个网络备份服务部分,其中允许用户下载一个 zip 文件,其中包含他们从备份的列表中选择的文件。

压缩过程是通过 shell_exec 命令完成的。为此,我已授予 apache 用户 no-password sudo 对 zip 命令的权限。

我的问题是,因为我不能对包含用户备份文件的文件夹执行“sudo cd /path/to/user/files”,所以我需要指定要压缩的文件的完整绝对路径,然后进入我不想要的 zip 文件。

这就是我的意思:

用户“test1”备份了他们的文件,这些文件存储如下
/home/backups/test1/my pics/pic1.jpg
/home/backups/test1/my pics/pic2.jpg
/home/backups/test1/my图片/pic3.jpg
/home/backups/test1/hello.txt
/home/backups/test1/music/band/song.mp3

当这些文件被压缩时,它们会保留完整的路径,但我只想显示:
my pics/pic1.jpg
my pics/pic2.jpg
my pics/pic3.jpg
hello.txt
music/band/ song.mp3

有没有办法告诉 zip 截断路径,或者为每个文件指定一个自定义路径?
或者有什么方法可以将工作目录更改为用户的根备份文件夹,这样我就可以简单地指定相对文件路径。

感谢您的阅读,希望您能走到这一步,这一切都是有道理的!

4

4 回答 4

2

I have to slightly question why you are making a potentially dangerous system shell call when there are a number of good PHP zipping classes around. The tutorial here shows how to easily create a class that will output a zip file to the browser. There is also a number of classes on phpclasses.org, this seems to be the best one.

If you have to do it with a system call my suggestions are:

To truncate the path for the file you could use symbolic links.

Can you not increase the permissions of the zip executable to mean that apache can use it without using sudo?

于 2009-05-05T15:46:00.163 回答
1

一个更好的主意可能是制作一个 shell 脚本,并授予网络服务器 sudo 访问该 shell 脚本的权限。这也可能更安全

#!/bin/bash
#
# Zip up files for a given user
# 
# usage: backupToZipFile.sh <username>

cd home/backups/$1
tar -czf /tmp/$1.tgz .

您是否还考虑过以尝试压缩文件的用户身份运行 sudo 而不是 root 用户?这也会更安全。

于 2009-05-06T23:03:53.707 回答
1

您是否尝试过使用chdir()更改工作目录?

chdir('/home/backups/test1/');
于 2009-05-05T15:08:00.940 回答
0

这似乎是一个简单的选择,但根据man它只是不存在。您当然可以将要存档的内容符号链接到您将创建 zip 文件的位置(我假设您实际上可以 cd 到该目录)。使用正确的选项,zip将不会归档符号链接本身,而是使用符号链接的名称归档符号链接文件。

例如,创建符号链接 /tmp/$PID/my pics/pic1.jpg 等,然后将所有内容压缩到 /tmp/$PID。

于 2009-05-05T15:29:43.150 回答