Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP+;TAR来提取文件夹_Php_Tar - Fatal编程技术网

使用PHP+;TAR来提取文件夹

使用PHP+;TAR来提取文件夹,php,tar,Php,Tar,我想自动将最新的WordPress版本下载到Web服务器的根文件夹。以下是我目前的代码: <?php $fp = fopen (dirname(__FILE__) . '/wp.tar.gz', 'w+'); $ch = curl_init('http://wordpress.org/latest.tar.gz'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); cu

我想自动将最新的WordPress版本下载到Web服务器的根文件夹。以下是我目前的代码:

<?php
$fp = fopen (dirname(__FILE__) . '/wp.tar.gz', 'w+');
$ch = curl_init('http://wordpress.org/latest.tar.gz');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
exec('tar -zxvf wp.tar.gz wordpress');
unlink('wp.tar.gz');
?>

问题是,WordPress tar文件包含一个包含所有文件的目录/WordPress,因此在运行这个PHP文件之后,我现在有一个包含这些文件的/WordPress目录,但是我希望它位于/(PHP文件运行的位置),而不是/WordPress中的所有内容。或者我应该使用“mv”命令将所有内容从/wordpress移动到/

谢谢你

为什么不使用这个:
Why dont you use this : 
http://wordpress.org/latest.zip

Then your code would look like:

<?php
$filename = "http://wordpress.org/latest.zip";
$contents = file_get_contents($filename);
$latestzip_file = 'latest.zip';
$fh = fopen($latestzip_file, 'w') or die("can't open file");
fwrite($fh, $contents);
fclose($fh);
$zip = new ZipArchive;
$res = $zip->open($latestzip_file);
if($res === TRUE)
{
      $zip->extractTo(dirname(__FILE__));       // Extract to the main directory //
  $zip->close();
      unlink($latestzip_file);
    }
?>
http://wordpress.org/latest.zip 那么您的代码将如下所示:
为什么不使用这个:
http://wordpress.org/latest.zip
那么您的代码将如下所示:

如果您使用的是足够新的GNU或BSD tar版本:

exec('tar -zxvf --strip-components=1 wp.tar.gz wordpress');
顺便说一下,如果不需要提取文件的列表,则不需要
v
选项


另请参阅:

如果您使用的是足够新的GNU或BSD tar版本:

exec('tar -zxvf --strip-components=1 wp.tar.gz wordpress');
顺便说一下,如果不需要提取文件的列表,则不需要
v
选项

另请参见:

尝试添加
--strip=1
opt

  tar xvzf wp.tar.gz --strip=1
尝试添加
--strip=1
opt

  tar xvzf wp.tar.gz --strip=1