执行 php 解压缩文件的页面(操作文件)
execute php page that unzip a file (manipulate files)
我正在使用本地 (rhel 8) 服务器使用 php 解压缩文件(建议的解决方案:)
<?php
// assuming file.zip is in the same directory as the executing script.
$file = 'temperatures.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "Success! $file extracted to $path";
} else {
echo "Failled! I couldn't open $file";
}
?>
问题是:
- 当我使用以下命令在服务器上执行此代码时:
php /var/www/html/unzip.php
它工作正常(成功消息),我在同一目录中找到提取的文件。
- 但是当我使用浏览器 (http://192.168.1.5/unzip.php) 时打印了成功消息,但我无法在此目录中找到解压缩的文件。
注意:我试过使用 wamp 服务器,它可以工作,所以问题可能出在服务器上。
顺便说一下,所有操作文件的 php 行代码都没有生效。我通过允许 Apache 用户(rhel 中的apache
)在目录中创建文件来解决这个问题。这可以通过使 Apache 成为目录 运行 的所有者来完成此命令:
sudo chown apache /var/www/html/
我正在使用本地 (rhel 8) 服务器使用 php 解压缩文件(建议的解决方案:)
<?php
// assuming file.zip is in the same directory as the executing script.
$file = 'temperatures.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "Success! $file extracted to $path";
} else {
echo "Failled! I couldn't open $file";
}
?>
问题是:
- 当我使用以下命令在服务器上执行此代码时:
php /var/www/html/unzip.php
它工作正常(成功消息),我在同一目录中找到提取的文件。 - 但是当我使用浏览器 (http://192.168.1.5/unzip.php) 时打印了成功消息,但我无法在此目录中找到解压缩的文件。
注意:我试过使用 wamp 服务器,它可以工作,所以问题可能出在服务器上。
顺便说一下,所有操作文件的 php 行代码都没有生效。我通过允许 Apache 用户(rhel 中的apache
)在目录中创建文件来解决这个问题。这可以通过使 Apache 成为目录 运行 的所有者来完成此命令:
sudo chown apache /var/www/html/