MrTindervox
MrTindervox

Reputation: 481

Laravel Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

After creating a fresh Laraver project using:

laravel new [project_name]

At first it ran, but after running it for the second time I got an error saying:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

Fatal error: Unknown: Failed opening required 'D:[path]\server.php' (include_path='C:\xampp\php\PEAR') in Unknown on line 0

Upvotes: 15

Views: 41075

Answers (9)

Akshayffb
Akshayffb

Reputation: 43

I had the issue with Laravel 8 after cloning a repository. The server.php file in the root directory was missing. To resolve this, I created the file and added the following code, which worked for me.

<?php

$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
return false;
}
require_once __DIR__ . '/public/index.php';

?> 

Upvotes: 0

Haseeb Khalid
Haseeb Khalid

Reputation: 1

The path to the server.php file

vendor\laravel\framework\src\Illuminate\Foundation\resources\server.php

If you are facing the error that means the file is not avaliable, so you have to create it by yourself and paste the code:

    <?php

$publicPath = getcwd();

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists($publicPath.$uri)) {
    return false;
}

require_once $publicPath.'/index.php';
?>

Upvotes: 0

crit_x
crit_x

Reputation: 11

The Avast antivirus removed the file, open avast app

  • Go to protection
  • Restore the file (Don't press delete)

Upvotes: 0

B Baheer
B Baheer

Reputation: 11

you should add this(server.php) file into your laravel project

<?php

$publicPath = getcwd();

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists($publicPath.$uri)) {
    return false;
}

require_once $publicPath.'/index.php';
?>

in this address(vendor\laravel\framework\src\Illuminate\Foundation\resources); and add it to exceptions of antivirus that you use it

Upvotes: 0

Kenneth Kimani
Kenneth Kimani

Reputation: 21

That's because you are probably using Avast antivirus, The solution is simply open the Antivirus-check virus and threats, make server.php an exception and try open the localhost again, it should now work

Upvotes: 2

Kiran Maniya
Kiran Maniya

Reputation: 8979

Seems that you have deleted the server.php file from the root of the Laravel project. You can re-create new server.php and put the PHP script as given,

<?php
    $uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
    if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
        return false;
    }
    require_once __DIR__.'/public/index.php';

or you can go to the official laravel github repo to grab the original server.php file.

Upvotes: 2

Vladimir Salguero
Vladimir Salguero

Reputation: 5947

This error occurs when the server.php file is needed in the root directory of the project, if you need it you can create the file and basically this is the code that must contain.

server.php

<?php
$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}
require_once __DIR__.'/public/index.php';

Upvotes: 9

edgan
edgan

Reputation: 1

I'm on windows 10 also and I had the same issue and error message:

PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

PHP Fatal error: Unknown: Failed opening required 'D:\yourProject\server.php' (include_path='C:\xampp\php\PEAR') in Unknown on line 0

How to solve this. On Avast antivirus:

  • Add route exception to your project path

  • Check if the server.php file exists on your root path project, if not it means that antivirus removed it, go to the Avast trunk and restore it.

Stop server and run it again.

Upvotes: 0

MrTindervox
MrTindervox

Reputation: 481

The problem was that the initial directory included server.php file and the second time around it was missing.

For me this was a weird interaction with Avast as it perceived the file as malicious. Check Avast's Virus chest to recover the file to avoid further issues.

Maybe this will save time for somebody.

Upvotes: 23

Related Questions