< Referring to Make path accessible at various level of folder on PHP >
The code below:
require_once __DIR__ . '/../FolderB2/PageB21.php';
This works locally, but not when I have uploaded the page to the server. Is my hosting company restricting access to this?
If you run this code, do you get the exact path you are expecting?
echo dirname(__FILE__) . '/../FolderB2/PageB21.php';
Perhaps the relative path is wrong or maybe there is a mistake in the casing (i.e. if your local machine is windows, paths are not case sensitive, but they would be on a Linux server).
Perhaps you can use $_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php' if your host runs PHP less than 5.3
Related
For some reason this path works perfectly on one developer's machine
include dirname(__FILE__) . '/../includes/init.php';
But on my machine I have to remove the '/..' section for it to work.
include dirname(__FILE__) . '/includes/init.php';
I suspect this is linked other issues with the application on my machine.
Why is this occurring and what can I change to get it working like it does on the original developer's machine?
When I encountered something similar, I used the getcwd() function in PHP to see that the working directory is different.
I managed to solve it by chdir to the current directory at start, and the relative paths have been working ever since.
Or you can use absolute paths.
http://php.net/manual/en/function.chdir.php
You could check the DOCUMENT_ROOT on each of the dev machines. Ensuring you're working from the same directory etc.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
echo $path;
?>
I am new to php.
I am using below code to upload image in local folder path "uploads/profilepics/".
move_uploaded_file
(
$_FILES["file"]["tmp_name"],
"uploads/profilepics/".$filenameWithUsername
);
It is working fine.
In remote server, my folder structure look like this, /public_html/myfamily/uploads/profilepics
I would like to know a good approach to store the image in remote server, if the code need some improvement?
I would use the __DIR__ magic constant (if youre lower than php 5.3 dirname(__FILE__)) to make sure your path is the same on local as on your server.
This will give you a fullpath to your dir no matter on what machine you are running.
So in your case id say try:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
__DIR__ . "/uploads/profilepics/".$filenameWithUsername
);
if it doesn't work
echo __DIR__ . "/uploads/profilepics/".$filenameWithUsername;
See what it shows and edit the path to where it needs to go if it isn't correct
Since I develop on localhost but deploy elsewhere, and since I don't want to have to force my sites to be under a Windows partition's root directory (currently F:\web_dev\htdocs), code like this:
require_once($_SERVER['DOCUMENT_ROOT'] . '/projXY/database/database_common.php');
OdbcExec($sql); // defined in the file above
causes Netbeans to issue a "Warning: unknown function".
Now, I could get round this by using a directory structure like :
F:\project_1
F:\project_2
instead of
F:\web_dev_htdocs\project_1
F:\web_dev_htdocs\project_2
and then using
require_once('/database/database_common.php');
BUT that imposes constraints on where the end-user an install my site.
Simplest by far would be to tell NetBeans which local directory corresponds to $_SERVER['DOCUMENT_ROOT'], but I can't find a configuration option for that. I am sure this is a common problem. Any suggestions?
Update: NetbBeans v7.0.1
Simply add the /path/to/projXY/database/ directory to your project's include path. Netbeans will then pick up the files there and use them as code references.
http://netbeans.org/kb/docs/php/project-setup.html#phpIncludePath
Addendum
Relying on $_SERVER['DOCUMENT_ROOT'] is generally a bad idea. For one, it eliminates the ability to run parts of your application via the console / command line.
You should instead either use configurable, absolute paths to shared libraries or do as in Brandon's answer and use a relative path from __DIR__ (PHP 5.3) or dirname(__FILE__)
Not sure if this is what you are looking for or not, but I commonly use:
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'file.php');
I just copied the library folder on the root directory of my net server . i.e. /var/www .
and used
require ( 'GChartPhp/gChart.php' ) ;
in my code on the file graph.php.
ERROR : But, for some reason , the browsers do not load this (graph.php) file .
However, I did the same on my local wamp server and my I am able to run the file.
I have no idea what I need to additionally do for this linux / apache server .
Thanks
I suppose the include_path is not configured the same way on your two servers : you might have to add either the WAMP equivalent of /var/www or . to it.
Modifying the include_path can be done by editing your php.ini file, or using set_include_path() at the beginning of your scripts.
Another possible solution would be to use an absolute path to the file you are including -- which can be done using something like this :
include dirname(__FILE__) . '/GChartPhp/gChart.php';
Notes :
__FILE__ will correspond to the full path to the file in which you write it
And using dirname() on it will get you the path to the directory which contains that file.
Which means that this line will use an absolute path... but written relatively to the file in which you put that line.
So I had an issue last night when I went to upload a project I've been working on locally to my server.
I had this:
require_once "/../controllers/source/MySpaceID/myspace.php";
Which is the correct path to that file finding its way out of the libraries folder. It worked fine until I put it on the server. Any thoughts?
Echo you current working directory on the server ( echo getcwd(); ) and check your path from there, that's probably where your problem lies.
It sounds strange to me that CI would use ./library as its working directory - unless you set it yourself.
Note that you should use code igniter's APPPATH constant to create absolute paths instead of using relative ones, this will make things easier for you on the long term.
Lepidosteus is correct. Your library should live in /application/library/ and you should require it like this require_once APPPATH.'/libraries/MySpaceID/myspace.php'