phpgoal Posted September 5, 2013 Share Posted September 5, 2013 Hi, I am using file zilla to upload file. I have files as below: folder1 folder1file1.php foder2 folder2file.php folder3 folder3file.php Now i am working with folder3file.php. In php, i want to include folder1file.php. How can i do it? <?php 'include 'folder1/folder1file1.php'; ?> not working. Could you please help? -P Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/ Share on other sites More sharing options...
phpgoal Posted September 5, 2013 Author Share Posted September 5, 2013 Also, if you could provide me any link where i can learn it. it would be great. I did search but could not find something specific. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/#findComment-1448333 Share on other sites More sharing options...
requinix Posted September 5, 2013 Share Posted September 5, 2013 Use absolute paths whenever possible, either using the DOCUMENT_ROOT or __DIR__. // works regardless of where you are include $_SERVER["DOCUMENT_ROOT"] . "/folder1/folder1file1.php"; // have to know the relative path between the current file and the one you want include __DIR__ . "/../../folder1file1.php"; Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/#findComment-1448336 Share on other sites More sharing options...
AbraCadaver Posted September 5, 2013 Share Posted September 5, 2013 I and many others would not do this. I would use a file in the top most directory to control the application and include other files. However, to do this you either need to give the relative path from where you are, or the absolute path: //two ways for relative include('../../folder1file1.php'); include('../../../folder1/folder1file1.php'); //example absolute include('/var/www/vhosts/domain/httpdocs/folder1/folder1file1.php'); Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/#findComment-1448337 Share on other sites More sharing options...
Barrikor Posted September 5, 2013 Share Posted September 5, 2013 Maybe what you'll want to do is just change the folder PHP includes files from? If you're running a PHP file in folder1/folder2/folder3 then PHP is looking for files from folder3's location, which I doubt is what you really want. You probably want to change the running directory to the folder that your folder1 is in. The function for that is chdir(). http://php.net/manual/en/function.chdir.php so if you're 3 folders up from where you want to be, chdir() down 3 folders: chdir('../../../'); Now your includes should all work the way you wanted on your post: include 'folder1/folder1file1.php'; Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/#findComment-1448341 Share on other sites More sharing options...
phpgoal Posted September 5, 2013 Author Share Posted September 5, 2013 worked perfectly. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/281898-file-directory/#findComment-1448344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.