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 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码