在 PHP 中通过 shell_exec 压缩文件:路径问题

发布于 2024-07-19 03:17:10 字数 786 浏览 3 评论 0原文

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

压缩过程是通过 shell_exec 命令完成的。 为此,我向 apache 用户授予了 zip 命令的无密码 sudo 权限。

我的问题是,由于我无法对包含用户备份文件的文件夹执行“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 pics/pic3.jpg
/home/backups/test1/hello.txt
/home/backups/test1/music/band/song.mp3

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

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

感谢您的阅读,希望您能读到这里并且一切都有意义!

I'm 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 what they have backed up.

The zipping process is done via a shell_exec command. For this to work I have given the apache user no-password sudo privileges to the zip command.

My problem is, since I can't do "sudo cd /path/to/user/files" to the folder containing the user's backed up files I need to specify the full absolute path to the files to be zipped which then go into the zip file which I don't want.

Here's what I mean:

The user, 'test1' has their files backed up which are stored as follows
/home/backups/test1/my pics/pic1.jpg
/home/backups/test1/my pics/pic2.jpg
/home/backups/test1/my pics/pic3.jpg
/home/backups/test1/hello.txt
/home/backups/test1/music/band/song.mp3

When these files are zipped up they keep the full paths but I just want it to show:
my pics/pic1.jpg
my pics/pic2.jpg
my pics/pic3.jpg
hello.txt
music/band/song.mp3

Is there some way to tell zip to truncate the paths, or specify a custom path for each file?
Or is there some way I can change the working directory to the user's root backup folder so I can simply specify relative file paths.

Thanks for reading, hope you got this far and it all makes sense!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韵柒 2024-07-26 03:17:10

我不得不稍微质疑一下,当周围有许多好的 PHP 压缩类时,为什么您要进行潜在危险的系统 shell 调用。 教程展示了如何轻松创建一个将 zip 文件输出到浏览器的类。 phpclasses.org 上也有很多课程,这似乎是最好的一个< /a>.

如果您必须通过系统调用来完成此操作,我的建议是:

要截断文件的路径,您可以使用 符号链接

能否不增加 zip 可执行文件的权限,让 apache 可以在不使用 sudo 的情况下使用它?

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?

滿滿的愛 2024-07-26 03:17:10

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

chdir('/home/backups/test1/');

Have you tried using chdir() to change the working directory?

chdir('/home/backups/test1/');
风吹雪碎 2024-07-26 03:17:10

更好的想法可能是创建一个 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 .

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

A better idea may be to make a shell script, and grant the webserver sudo access to that shell script. This may be more secure, too

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

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

Also have you considered running sudo as the user you're trying to zip files as, instead of as root? This would be more secure as well.

前事休说 2024-07-26 03:17:10

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

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

It seems like a simple option, but according to man it's just not there. You could of course symlink the stuff you want to archive in the location where you'll create the zip file (I assume you can actually cd to that directory). With the proper options, zip will not archive the symlinks themselves but the symlinked files instead, using the names of the symlink.

E.g. create symlinks /tmp/$PID/my pics/pic1.jpg etc, and then zip everything in /tmp/$PID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文