dousong2967 2012-08-12 20:34
浏览 115
已采纳

通过PHP exec()创建tgz文件

I'm trying to create a .tgz file through the PHP exec(); function in an AJAX web interface, however, my code isn't making any files, and it isn't writing out any errors.

This code works in shell:

tar cvfz destination/testfolder.tgz source/testfolder

I'm setting the destination/testfolder.tgz and the source/testfolder as variables in PHP since they change based on user input, so my code looks something like this:

$q=$_GET["q"];
$part = explode("/", $q);
$source = $q;
$dest = "destination/" .$part[1].".tgz";
exec("tar cvfz " . $dest . " " . $source);

I've echoed the "tar cvfz " . $dest . " " . $source and copy/pasted it into shell, which works, so I'm confused as to what I'm doing wrong.

  • 写回答

1条回答 默认 最新

  • douqudi5282 2012-08-12 20:42
    关注

    The problem is, for the same security issue @David mentioned, the PHP exec() function has very limited permissions - it can't even create files. Instead of chmodding the file to 777, which is very dangerous even temporarily, I suggest you create a temporary folder either in a sandbox or at any non-harmful location then ch own it to the user/group of the PHP interpreter's user/group with somewhat restrictive permissions (e. g. chmod 700 for the directory, 600 for the files inside).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题