Please help!
My directory structure is this:
my_application
my_application/index.php
my_application/images
my_application/phpcode/my_script.php
I have this code in my_script.php
<?php
if(file_exists('../images)){
// do something
}
?>
It works in wamp perfectly!!!
But when I upload it to my server (hostgator), it doesn't work.
In my server I do not upload it to root folder, but to a subfolder.
rootfolder/folder1/folder2/my_application
From what I understand, .. is in reference to the root path, not the current file.
How can I reference the current's file parent's folder images?
Sorry for my English. Please help! I'm stuck on this one.
You can simpli use dirname($SCRIPT_FILENAME) to reference the direcotry of your current script.
Have a look at PHP Constant and PHP Reserved Variable :)
Hope this help
Related
I have a file structure that looks like this (unimportant files left out):
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
Now when I try to call require_once('../PHP/functions.php'); inside the "index.php" it doesn't work and I don't understand why. Anyone an idea what the problem could be?
This works fine:
require_once('../file.php');
first thanks for all the answers, I found the bug now:
inside the functions.php I require another file and the path to that file wasn't correct when I called it from index.php
So I had to change the path inside functions.php not inside index.php.
Still thanks to everyone for the other suggestions :)
Do you get an PHP error, and if what does it say?
Check for lowercase/uppercase problems
The following should work correctly:
require_once('../PHP/functions.php');
Check your privileges on the folder and try:
require_once('../PHP/functions.php')
You have a Folder Structure Like so:
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
And then, you are inside of index.php trying to Access the File functions.php, which lives inside of the Directory PHP. Then you need to go up one Directory from the current directory of the index.php File (ie. The Administration Directory) [meaning that you get up to Testserver Folder from theAdministration Folder] and then drill down to the PHP Directory. In code this would mean:
<?php
require_once __DIR__ . "/../PHP/functions.php";
Hope this does it for you.
Cheers & Good Luck....
I have a user_ip_location.php file in "/user/files/new" folder.
full url to the file is
/user/files/new/user_ip_location.php
I want to include this file to profile.php file that is located in Root dir
I have tried the following code
include("/././user_ip_location");
and
include("http://example.com/user/files/new/user_ip_location");
Both are not working, first is returning no such file or directory and the second is not loading the page and the browser is returning error could not locate to remote server
Is there any way to fix it?
You need to indicate previous directory by double dots ..
include("../../user_ip_location.php");
If I understand your question correctly you are in some directory and want to include a file from /user/files/new/user_ip_location.php. So just including it by specifying the path should do the trick.
In your example this would be:
include("/user/files/new/user_ip_location.php");
If the file you want to include is levels above your current directory just specify this by using .. like include("../../user_ip_location.php");
Try this:
include './files/new/user_ip_location.php';
I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.
have a website that is obviously in the root of my server. I am now making a web app, which lies in m/iphone/. All was going well but my website uses scandir() with all of the files in a folder on the root of my server.
How can I access these files from when I'm inside the m/iphone directory?
Sorry if it's a really obvious answer, I'm a complete beginner.
Thanks in advance for any help
You have to use .. which stands for back in terms of folders, in your case you should run something like this:
scandir(dirname(__DIR__));
You should play with .. in path name:
scandir(dirname(__FILE__).'../anotherfolder'); //go back one folder and go to folder 'anotherfolder'
Hope it helps.
I need help with writing a php code that allows you to locate files outside a current folder.
supposing I have this directory
......./folders/codes
where my current file index.php is but then I want to reference a file in folders via the file in code, how do I go about that?
I thought i could use the HTML/CSS way where ../ would move you down one folder but it did not work when i tried it
require "../Login.php";------------------it failed
dirname return you the path up to your parent directory.
Try to use following code
require dirname(dirname(__FILE__)).'Login.php';
You sure your path is correct?
A common mistake is to overlook from where you're require-ing.
If, for example, you were to have a script that was included in another script, the relative path would not be the one you'd think it would be.
Try
echo realpath(dirname(__FILE__));
To see if you didn't make that mistake.
Not sure if this is appropriate, but it didn't fit in a comment.