문제

I'm working on a community written in PHP. I was just going to start working on a chat, so I created a new folder within the root one called "chat". And in that chat folder I created an "index.php" file. In that index.php file I wanna

require_once '../core/init.php';

The require_once fucntion works, but within my init.php file I

require_once 'functions/sanitize.php'.

this gives an error

Warning: require_once(functions/sanitize.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\ooplr\core\init.php on line 26

Line 26 is

require_once 'functions/sanitize.php'.

Any ideas?

https://i.stack.imgur.com/h2mtQ.png

Regards,

Gustaf

도움이 되었습니까?

해결책

The best thing to do in a project like this is to define a constant pointing to the current working directory and then use it throughout your app.

For example, consider the following:

chat/index.php

<?php

// Constants
define('CWD', getcwd()); // points to C:/wamp/www/ooplr/chat

// Load Init Script
require CWD .'/../core/init.php'; // points to C:/wamp/www/ooplr/core/init.php

// Rest of code ...

?>

core/init.php

<?php

// Load Sanitizer
require CWD .'/../functions/sanitize.php' // points to C:/wamp/www/ooplr/functions/sanitize.php

// Rest of the code

?>

etc...


Alternatively...

chat/index.php

<?php

// Constants
define('BaseDir', getcwd() .'/../'); // points to C:/wamp/www/ooplr/

// Load Init Script
require BaseDir .'core/init.php'; // points to C:/wamp/www/ooplr/core/init.php

// Rest of code ...

?>

core/init.php

<?php

// Load Sanitizer
require BaseDir .'functions/sanitize.php' // points to C:/wamp/www/ooplr/functions/sanitize.php

// Rest of the code

?>

다른 팁

Make sure the file exists and is in the right directory. As far as I recall, require (and include) will include the file starting from the "request"-directory. So if you call init.php, require will search in core/functions/sanitize.php.

you have core/init.php and functions/sanitize.php so need to use path back to one dir and include the file .

try

require_once '../functions/sanitize.php';

sanitize.php is in function directory and function directory is not in core folder
Now change

require_once 'functions/sanitize.php';

to

require_once '../functions/sanitize.php';

Asp your folder structure image functions/sanitize.php file not in core folder. so use this code.

require_once '../functions/sanitize.php';
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top