Failed to open stream: No such file or directory in. Localhost x Locaweb

Asked

Viewed 1,422 times

-2

I made an application on my machine using the Wampserver server. However, uploading this application to Locaweb causes the error below. The PHP version of Locaweb is 7.1.

Warning: include_once(/include/head.php): failed to open stream: No such file or directory in /home/Storage/3/46/ab/meusite/public_html/Dashboard.php on line 25

1 answer

1


Fábio, the server does not lie, so if it says that there is no file, then probably this file does not exist.

Your application index is probably located in the following folder on the Locaweb server:

/home/Storage/3/46/ab/meusite/public_html/index.php

So when doing include this way:

<?php
include_once('/include/head.php')
?>

The server will look for a include folder on the server root and not on the application root:

/include/head.php

You can make the include head php. using the magic constant __DIR__ or the function getcwd, which will return the basic directory of the file being executed, or with the DOCUMENT_ROOT (when present), which also points to the root of the application.

+- so in the index php.:

<?php
include_once(getcwd() . '/include/head.php');

include_once(__DIR__ . '/include/head.php');

include_once($_SERVER['DOCUMENT_ROOT'] . 'include/head.php');
?>

A good practice, is to define a constant with the base directory of your application and then use whenever you make a include.

index php.

<?php
define('APP_PATH', getcwd());

include_once(APP_PATH . '/include/head.php');
?>

I hope I’ve helped.

  • 1

    Depending on which page server, you also have DOCUMENT_ROOT in $_SERVER, depending on the author’s intention (in case he needs include in several different directories, which is __DIR__ and getcwd() do not solve)

  • Yes, if you use DIR in a different directory will probably not work (in index yes). If the executed file is always index.php, getcwd() will work independently of the directory. DOCUMENTE_ROOT really is a valid alternative.

  • I tried all the possibility that you told me and it didn’t work. I put the includes in the index root and it worked.

  • friend, post the code with the error to help better. Already tried with DOCUMENT_ROOT?

  • If you can post the folder structure, the file that is running first (probably index.php) and which file you are trying to include, it helps a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.