Laravel 5-PHP警告:ZipArchive::extractTo():无效或单元化的Zip对象


Laravel 5 - PHP Warning: ZipArchive::extractTo(): Invalid or unitialized Zip object

我在创建新的laravel项目时遇到了一个小问题。昨天我创建了一个没有问题的新项目,但今天我得到了这个错误:

PHP Warning:  ZipArchive::extractTo(): Invalid or unitialized Zip object in /home/tomas/.composer/vendor/laravel/installer/src/NewCommand.php on line 114
PHP Warning:  ZipArchive::close(): Invalid or unitialized Zip object in /home/tomas/.composer/vendor/laravel/installer/src/NewCommand.php on line 116

我以为我没有/home/tomas/.comporter/vendor/laraavel/installer/src/NewCommand.php的权限,但我拥有所有权限。

你不知道哪里会有问题吗?非常感谢。

编辑:

我刚运行命令:laravel new projectName

<?php namespace Laravel'Installer'Console;
use ZipArchive;
use Symfony'Component'Process'Process;
use Symfony'Component'Console'Input'InputArgument;
use Symfony'Component'Console'Input'InputInterface;
use Symfony'Component'Console'Output'OutputInterface;
class NewCommand extends 'Symfony'Component'Console'Command'Command {
/**
 * Configure the command options.
 *
 * @return void
 */
protected function configure()
{
    $this->setName('new')
            ->setDescription('Create a new Laravel application.')
            ->addArgument('name', InputArgument::REQUIRED);
}
/**
 * Execute the command.
 *
 * @param  InputInterface  $input
 * @param  OutputInterface  $output
 * @return void
 */
protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->verifyApplicationDoesntExist(
        $directory = getcwd().'/'.$input->getArgument('name'),
        $output
    );
    $output->writeln('<info>Crafting application...</info>');
    $this->download($zipFile = $this->makeFilename())
         ->extract($zipFile, $directory)
         ->cleanUp($zipFile);
    $composer = $this->findComposer();
    $commands = array(
        $composer.' run-script post-install-cmd',
        $composer.' run-script post-create-project-cmd',
    );
    $process = new Process(implode(' && ', $commands), $directory, null, null, null);
    $process->run(function($type, $line) use ($output)
    {
        $output->write($line);
    });
    $output->writeln('<comment>Application ready! Build something amazing.</comment>');
}
/**
 * Verify that the application does not already exist.
 *
 * @param  string  $directory
 * @return void
 */
protected function verifyApplicationDoesntExist($directory, OutputInterface $output)
{
    if (is_dir($directory))
    {
        $output->writeln('<error>Application already exists!</error>');
        exit(1);
    }
}
/**
 * Generate a random temporary filename.
 *
 * @return string
 */
protected function makeFilename()
{
    return getcwd().'/laravel_'.md5(time().uniqid()).'.zip';
}
/**
 * Download the temporary Zip to the given file.
 *
 * @param  string  $zipFile
 * @return $this
 */
protected function download($zipFile)
{
    $response = 'GuzzleHttp'get('http://cabinet.laravel.com/latest.zip')->getBody();
    file_put_contents($zipFile, $response);
    return $this;
}
/**
 * Extract the zip file into the given directory.
 *
 * @param  string  $zipFile
 * @param  string  $directory
 * @return $this
 */
protected function extract($zipFile, $directory)
{
    $archive = new ZipArchive;
    $archive->open($zipFile);
    $archive->extractTo($directory);
    $archive->close();
    return $this;
}
/**
 * Clean-up the Zip file.
 *
 * @param  string  $zipFile
 * @return $this
 */
protected function cleanUp($zipFile)
{
    @chmod($zipFile, 0777);
    @unlink($zipFile);
    return $this;
}
/**
 * Get the composer command for the environment.
 *
 * @return string
 */
protected function findComposer()
{
    if (file_exists(getcwd().'/composer.phar'))
    {
        return '"'.PHP_BINARY.'" composer.phar';
    }
    return 'composer';
}
}

检查安装过程中是否存在无法用英语读取的字符,如à或ö。


示例:C: ''Users''User''Informàtica''Web项目-->必须-->C: ''Users''User''Informatica''Web Projects

最近我也遇到了这个错误,但当我几周前安装它时,它很好,我对此进行了一些研究,但没有发现任何帮助。后来我尝试了以下方法:

  • 在ur cmd[composer global require"laravel/installer=~1.1"]中再次运行此命令

  • 它将显示:"已将当前目录更改为C:''Users/(your_profile_name)/AppData/Roaming/Composer./Composer.json"

  • 它将在更新完成后再次进行检查。它会很好用的。

  • 我的是这样工作的。

我可以调试并解决这个问题。对我来说,这是一个网络问题(防火墙)!

我观察到命令"laravel new"在我的家庭网络中运行良好,但我在办公网络中出现以下错误

PHP Warning:  ZipArchive::extractTo(): Invalid or unitialized Zip object in C:'Users'Rajesh Kumar Raj'AppData'Roaming'Composer'vendor'laravel'installer'src'NewCommand.php on line 157

因此,我决定调试NewCommand.php文件。

我注意到以下函数试图通过调用URL来下载zip文件http://cabinet.laravel.com/latest.zip

protected function download($zipFile, $version = 'master')
{
    switch ($version) {
        case 'develop':
            $filename = 'latest-develop.zip';
            break;
        case 'master':
            $filename = 'latest.zip';
            break;
    }
    $response = (new Client)->get('http://cabinet.laravel.com/'.$filename);
    file_put_contents($zipFile, $response->getBody());
    return $this;
}

我尝试在浏览器中键入URL,但由于URL包含.zip扩展名,因此出现了防火墙错误。所以,我要求我的网络管理员排除对该URL的检查。

然后项目成功创建,没有任何问题。

因此,解决方案是,尝试访问URL(http://cabinet.laravel.com/latest.zip)从您的浏览器。如果不起作用,请联系网络管理员或尝试从家庭/移动网络执行命令。希望这能有所帮助。非常感谢。

我只是在当前路径中创建了一个文件夹来使用该命令,它类似于:

laravel新larvIinstall

其中larvInstall既不是路径,也不是命令选项,只是文件夹的名称。而且效果很好。