Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP require_once error when including file in parent folder

I have the following file structure

-- Plugins
-- inParent.php
---- Uploadify
------ inSame.php
------ Uploadify.php

This function was working smoothly till yesterday. But now, If I try to include the inParent.php in uploadify.php using require_once('../inparent.php), It giving me a silly error..saying...

Warning: require_once(../inParent.php) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/plugins/uploadify/uploadify.php on line 3  

But the file definitely exists...

The same code when used to include inSame.php shows no error and works perfectly..

I guessed that there could be file permission problem.. But everything is fine..all the related files have 755/644 permissions.

Also, the move_uploaded_file function has also stopped working.

All the code was working fine till yesterday. I hav'nt changed anything.

I have tried everything and dont know what exactly happened..

Please help me friends.. :( :(

like image 500
Sharad Saxena Avatar asked Sep 03 '12 06:09

Sharad Saxena


People also ask

What is the difference between require_once and include in PHP?

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.

What does Require_once mean in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.

How do I move to a previous directory in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.


2 Answers

I've googled and found this : require_once __DIR__.'/../inParent.php';

like image 81
SpiderWan Avatar answered Oct 21 '22 13:10

SpiderWan


In my case I had to write:

require_once dirname( __FILE__ ) . '/' . '../../inParent.php';

And that worked!

like image 38
Hasnat Babur Avatar answered Oct 21 '22 12:10

Hasnat Babur