PHP解压函数可以正常工作,但如果您从httpdocs调用它,则无法正常工作/
我有点被一个问题缠住了,我希望这里有人能给我点启示 我正在使用一些函数解压.zip文件及其文件夹,效果很好。但是,如果我尝试从任何其他目录使用该函数,它似乎不起作用,或者似乎无法创建文件夹dir并写入文件 我正在使用以下代码解压缩.zip文件PHP解压函数可以正常工作,但如果您从httpdocs调用它,则无法正常工作/,php,Php,我有点被一个问题缠住了,我希望这里有人能给我点启示 我正在使用一些函数解压.zip文件及其文件夹,效果很好。但是,如果我尝试从任何其他目录使用该函数,它似乎不起作用,或者似乎无法创建文件夹dir并写入文件 我正在使用以下代码解压缩.zip文件 <?PHP error_reporting(e_all); include("../../includes/config.php"); /** * Unzip the source_file in the destination dir *
<?PHP error_reporting(e_all);
include("../../includes/config.php");
/**
* Unzip the source_file in the destination dir
*
* @param string The path to the ZIP-file.
* @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used
* @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
* @param boolean Overwrite existing files (true) or not (false)
*
* @return boolean Succesful or not
*/
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
if ($zip = zip_open($src_file))
{
if ($zip)
{
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
// Create the directories to the destination dir if they don't already exist
create_dirs($dest_dir);
// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Now we're going to create the directories in the destination directories
// If the file is not in the root dir
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false)
{
// Create the directory where the zip-entry should be saved (with a "/" at the end)
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// Open the entry
if (zip_entry_open($zip,$zip_entry,"r"))
{
// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);
// Check if the files should be overwritten or not
if ($overwrite === true || $overwrite === false && !is_file($file_name))
{
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
file_put_contents($file_name, $fstream );
// Set the rights
chmod($file_name, 0777);
echo "save: ".$file_name."<br />";
}
// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
}
}
else
{
return false;
}
return true;
}
/**
* This function creates recursive directories if it doesn't already exist
*
* @param String The path that should be created
*
* @return void
*/
function create_dirs($path)
{
if (!is_dir($path))
{
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory)
{
$directory_path .= $directory."/";
if (!is_dir($directory_path))
{
//mkdir($directory_path);
my_ftp_mkdir2('httpdocs/templates/temp/', $directory_path);
//chmod($directory_path, 0777);
}
}
}
}
/* CHMOD with FTP */
function my_ftp_mkdir2 ($path, $dir) {
$server = "ftp.domainname.com";
// Connect to FTP server
$connection = ftp_connect ($server);
// Login on FTP server
$user = "username";
$pass = "password";
$result = ftp_login ($connection, $user, $pass);
if ((!$connection) || (!$result)) {
return false;
}
// Go to the dir thats been given
if (!@ftp_chdir ($connection, $path)) {
ftp_close($connection); // close FTP connection
return false;
}
// Create the map and set the rights
if (!@ftp_mkdir($connection, $dir)) {
ftp_close($connection); // close FTP connection
return false;
}
$chmod_cmd = "CHMOD 0777 " . $dir;
$chmod = ftp_site($connection, $chmod_cmd);
ftp_close($connection); // close FTP connection
return true;
}
if (!unzip("../user1/test2.zip", false, true, true)) {
echo 'The file has failed to be unzipped!';
} else {
echo 'The file has been unzipped!';
}
?>
现在这个效果很好
但我正在测试目录中运行此文件,如下所示:
httpdocs/templates/temp/unzip.php
名为test2.zip的文件位于:
httpdocs/templates/user1/test2.zip
因此,当我运行脚本时,它会生成以下内容:
httpdocs/templates/user1/test2/index.html
httpdocs/templates/user1/test2/img/
等等
但是当我从httpdocs/目录运行这个脚本时,它说它已经解压了,但是没有创建任何dir或其他内容
我的PHP上有安全模式,这就是为什么我使用FTP将DIR修改为777进行测试
所以当我在httpdocs/unzip.php中运行这个脚本时
我这样调用函数
<?php
if (!unzip("templates/user1/test2.zip", false, true, true)) {
echo 'The file has failed to be unzipped!';
} else {
echo 'The file has been unzipped!';
}
?>
但这似乎不起作用,我希望有人能给我一些好的建议。谢谢 将httpdocs的所有者设置为apache及其www数据的Web服务器用户
chown www-data:www-data /path/to/htttpdocs -R
注意:我想你需要自己访问这些文件,所以用你的用户名交换第一个www数据,例如user123:www数据
注2:检查您的Web服务器的错误日志,您将在那里找到确切的错误消息。我只能猜测这是一个权限被拒绝的问题,Apache标准路径是/var/log/apache2/error.log
注3:在PHP5.4中,安全模式被删除,我建议您进行更新。me将使用绝对路径&
mkdir($abspathdir,0777)
&重置权限后的umask
可能更整洁更快有什么理由打开ftp连接来创建新文件夹吗
要调试,请添加
echo $abspathdir,'<br/>';
在mkdir之前,检查要创建的目录。如果路径确实正确,但未创建目录,则可能只是父文件夹的权限问题。谢谢您的回复,但httpdocs的所有者已被设置为webservers用户,没有任何区别。请尝试在函数调用中使用绝对路径。@Adrian您在错误中找到了一些有用的信息吗日志?我已经在$_服务器['DOCUMENT\u ROOT']上尝试过了,但是也给出了一条消息,zip被提取了,但是没有任何创建的目录的结果。。。我已经检查了错误日志,其中显示了如下错误:PHP警告:file\u put\u contents..etcetc。。第56行PHP警告:chmod..etcetc。。第58行第56行:文件内容$file\u名称$fstream;第58行chmod$file_name,0777;如果您深入查看错误日志和那些消息,您会发现您的问题如果我使用mkdir,它会创建dir,但它将所有者设置为apache,并且没有使用良好的权限对其进行chmod,这就是为什么我必须使用ftp函数的原因。我猜mkdir不能正常工作,因为safemode是打开的,这就是为什么我使用FTP函数来解决它。注意:启用安全模式时,PHP会检查脚本所在的目录是否与正在执行的脚本具有相同的UID所有者。你也可以试试pclzip LIB谢谢你的回复。但是我所有的目录都有所有者UID。