简体   繁体   中英

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

I have a little problem with creating new laravel project. Yesterday I created new project with no problem, but today I got this error:

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

I thought I don't have permission for /home/tomas/.composer/vendor/laravel/installer/src/NewCommand.php , but I have all rights.

Don't you know where can be problem? Thank you.

EDIT:

I just run command: 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';
}

}

Check out if in the route of the installation there's some character that it can not be read in english like à or ö.


Example: C:\\Users\\User\\Inform à tica\\Web Projects --> must be --> C:\\Users\\User\\Inform a tica\\Web Projects

Recently i also got that error but when i installed it few weeks earlier it was fine and i did some research on it but found nothing that could help much. Then i later tried follwing methods :

  • run this command again in ur cmd [ composer global require "laravel/installer=~1.1" ]

  • It will show : "Changed current directory to C:/Users/(your_profile_name)/AppData/Roaming/Composer ./composer.json has been updated "

  • It will update afterwards the update is complete check again. It will work fine.

  • Mine worked this way.

I can able to debug and resolve this issue. It turns out to be a network issue(firewall) for me!

I observed the command “laravel new ” is working fine in my home network but I am getting the following error in office network,

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

So, I decided to debug the NewCommand.php file.

I noticed the following function is trying to download a zip file by calling the URL 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;
}

I tried typing the URL in my browser and I got a firewall error since the URL includes a .zip extension. So, I asked my network administrator to exclude check for this URL.

Then the project got created successfully without any issues.

So, the solution is, try accessing the URL ( http://cabinet.laravel.com/latest.zip ) from your browser. If it's not working contact network admin or try executing the command from your home/mobile network. Hope this helps. Thank You.

I just used the command by creating a folder in the current path i was already in, it's something like:

laravel new larvIinstall

where larvInstall is not a path nor a command option, just the folder's name. and it worked just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM