I'm trying to adapt an upload script to fit on 000webhost. I keep getting errors about the destination folder not existing. This is what I have:
define('DESTINATION_FOLDER','/uploads/');
I've also tried
/public_html/uploads/
and
/subdomain/domain/com/uploads/
In examples I've seen people were using /www/ but I don't know where that goes.
What is the correct syntax to use in this case?
Try this:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].'/uploads/');
Consider using the PHP constant DIRECTORY_SEPARATOR, if platform independence matters to you:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
'uploads'.DIRECTORY_SEPARATOR);
http://php.net/manual/en/dir.constants.php
EDIT:
Storing uploads under the document root can be a security risk. Unless you want those files to be directly accessible by the web server, you should consider storing them outside of the document root within your webspace. If you created a folder named "uploads" along side your "public_html" folder with your host, you could access it like this:
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
'..'.DIRECTORY_SEPARATOR. // (up one level)
'uploads'.DIRECTORY_SEPARATOR);
Or just specify an absolute path (leading with /) directly to the folder.
Related
Whenever I use move_uploaded_file to my an uploaded file, the file always ends up in my web root. What am I doing wrong? Should the path be relative to my web root, or should it be an absolute path on my file system?
Ultimately what I'm trying to do, it have a folder for php to upload/dowload files. I don't want web bots and anyone else just to be able to access the files, i want only authenticated people using my website to be able to download the files. So this is how I have my file structure laid out:
/var/www/website/public_html
and
/var/www/website/files
and my move_uploaded_file command is like this:
move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "/var/www/website/files/".$_FILES['txtFileSelector']['name']);
but no matter what i've tried, the file always ends up in
/var/www/website/public_html
I've even tried sending the file in other sub folders of public_html but still no luck.
ah-ha! Destination path is relative!
So the solution for me is:
echo move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "../files/".$_FILES['txtFileSelector']['name']
Because of the relative pathing, use .../ to go up from the web root, and then move it to the desired storage folder.
CORRECTION
Absolute path or relative path either will work. It was a combination of folder permissions (www-data needs to either be owner or group member with read/write permissions) and me being an idiot and discovering a programming bug. My code was in a php class and the uploading was function. In my constructor I had a bug in my code. When doing OO there's a big difference between
$upload_dir = "/path/to/upload";
and
$this->upload_dir = "/path/to/upload";
I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.
I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here
I am developing a web application. contents are:
root dir (/var/www/)
config.php
index.php
details.php
admin dir (/var/www/admin)
admin.php
I have included config.php file into index.php, details.php in root directory using require_once('config.php') as this file contains database passwords, styles, images directory paths..
how can i include that config files in my admin/admin.php file so that one config file can be used in anywhere(even in subdirectories) of my web application. Will it make any difference for the value of define('APP_BASE_PATH', dirname(__FILE__)); when same config file is used by all files in the web application.
if i am wrong somewhere then please get me right.
If your server properly configured, just
include $_SERVER['DOCUMENT_ROOT']."/config.php";
anywhere
You have also 2 other possible ways.
a Front controller setup, where ALL user requests going into one file. And ths one going to include all others from their subdirectories. Personally I don't like it cause this front file become a mess. Though it's widely used.
I decided not to mention it because noone would use a hardcoded full path anyway.
Update after clarification in comments: You are looking for a way to include a central configuration file from anywhere in your project's folder structure.
#Col. Shrapnel shows one way, DOCUMENT_ROOT. It's the only way to use an "absolute" path from a nested folder structure. It has the limitation I describe above, but it's fine otherwise.
If you want maximum portability (i.e. the possibility to run the app with e.g. www.example.com/myapp/version_1 as its root directory), you would have to use relative references from within your folder structure to "climb down" to the config file, e.g. ../../config.php that will work reliably too, although be a bit cumbersome e.g. if you move a script to a different folder and you have to update the relative path.
you can use the same config file every time... using "/" will take you back to the root directory... so in admin/admin.php use this:
require_once("/config.php");
you can use "../" to take you up one directory eg:
require_once("../config.php");
was this what you were looking for?
In HTML, I can find a file starting from the web server's root folder by beginning the filepath with "/". Like:
/images/some_image.jpg
I can put that path in any file in any subdirectory, and it will point to the right image.
With PHP, I tried something similar:
include("/includes/header.php");
...but that doesn't work.
I think that that this page is saying that I can set include_path once and after that, it will be assumed. But I don't quite get the syntax. Both examples start with a period, and it says:
Using a . in the include path allows for relative includes as it means the current directory.
Relative includes are exactly what I don't want.
How do I make sure that all my includes point to the root/includes folder? (Bonus: what if I want to place that folder outside the public directory?)
Clarification
My development files are currently being served by XAMPP/Apache. Does that affect the absolute path? (I'm not sure yet what the production server will be.)
Update
I don't know what my problem was here. The include_path thing I referenced above was exactly what I was looking for, and the syntax isn't really confusing. I just tried it and it works great.
One thing that occurs to me is that some people may have thought that "/some/path" was an "absolute path" because they assumed the OS was Linux. This server is Windows, so an absolute path would have to start with the drive name.
Anyway, problem solved! :)
What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;
define( 'ROOT_DIR', dirname(__FILE__) );
Then in all files, I know what the root of my project is and can do stuff like this
require_once( ROOT_DIR.'/include/functions.php' );
Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.
One strategy
I don't know if this is the best way, but it has worked for me.
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/path/to/file.php");
The include_path setting works like $PATH in unix (there is a similar setting in Windows too).It contains multiple directory names, seperated by colons (:). When you include or require a file, these directories are searched in order, until a match is found or all directories are searched.
So, to make sure that your application always includes from your path if the file exists there, simply put your include dir first in the list of directories.
ini_set("include_path", "/your_include_path:".ini_get("include_path"));
This way, your include directory is searched first, and then the original search path (by default the current directory, and then PEAR). If you have no problem modifying include_path, then this is the solution for you.
There is nothing in include/require that prohibits you from using absolute an path.
so your example
include('/includes/header.php');
should work just fine. Assuming the path and file are corect and have the correct permissions set.
(and thereby allow you to include whatever file you like, in- or outside your document root)
This behaviour is however considered to be a possible security risk. Therefore, the system administrator can set the open_basedir directive.
This directive configures where you can include/require your files from and it might just be your problem.
Some control panels (plesk for example) set this directive to be the same as the document root by default.
as for the '.' syntax:
/home/username/public_html <- absolute path
public_html <- relative path
./public_html <- same as the path above
../username/public_html <- another relative path
However, I usually use a slightly different option:
require_once(__DIR__ . '/Factories/ViewFactory.php');
With this edition, you specify an absolute path, relative to the file that contains the require_once() statement.
Another option is to create a file in the $_SERVER['DOCUMENT_ROOT'] directory with the definition of your absolute path.
For example, if your $_SERVER['DOCUMENT_ROOT'] directory is
C:\wamp\www\
create a file (i.e. my_paths.php) containing this
<?php if(!defined('MY_ABS_PATH')) define('MY_ABS_PATH',$_SERVER['DOCUMENT_ROOT'].'MyProyect/')
Now you only need to include in every file inside your MyProyect folder this file (my_paths.php), so you can user MY_ABS_PATH as an absolute path for MyProject.
Not directly answering your question but something to remember:
When using includes with allow_url_include on in your ini beware that, when accessing sessions from included files, if from a script you include one file using an absolute file reference and then include a second file from on your local server using a url file reference that they have different variable scope and the same session will not be seen from both included files. The original session won't be seen from the url included file.
from: http://us2.php.net/manual/en/function.include.php#84052
hey all...i had a similar problem with my cms system.
i needed a hard path for some security aspects.
think the best way is like rob wrote. for quick an dirty coding
think this works also..:-)
<?php
$path = getcwd();
$myfile = "/test.inc.php";
/*
getcwd () points to:
/usr/srv/apache/htdocs/myworkingdir (as example)
echo ($path.$myfile);
would return...
/usr/srv/apache/htdocs/myworkingdir/test.inc.php
access outside your working directory is not allowed.
*/
includ_once ($path.$myfile);
//some code
?>
nice day
strtok
I follow Wordpress's example on this one. I go and define a root path, normally the document root, and then go define a bunch of other path's along with that (one for each of my class dirs. IE: database, users, html, etc). Often I will define the root path manually instead of relying on a server variable.
Example
if($_SERVER['SERVERNAME'] == "localhost")
{
define("ABS_PATH", "/path/to/upper/most/directory"); // Manual
}
else
{
define("ABS_PATH, dirname(__FILE__));
// This defines the path as the directory of the containing file, normally a config.php
}
// define other paths...
include(ABS_PATH."/mystuff.php");
Thanks - this is one of 2 links that com up if you google for php apache windows absolute path.
As a newbie to intermed PHP developer I didnt understand why absolute paths on apache windopws systems would be c:\xampp\htdocs (apache document root - XAMPP default) instead of /
thus if in http//localhost/myapp/subfolder1/subfolder2/myfile.php I wanted to include a file from http//localhost/myapp
I would need to specify it as:
include("c:\xampp\htdocs\myapp\includeme.php")
or
include("../../includeme.php")
AND NOT
include("/myapp/includeme.php")
I've come up with a single line of code to set at top of my every php script as to compensate:
<?php if(!$root) for($i=count(explode("/",$_SERVER["PHP_SELF"]));$i>2;$i--) $root .= "../"; ?>
By this building $root to bee "../" steps up in hierarchy from wherever the file is placed.
Whenever I want to include with an absolut path the line will be:
<?php include($root."some/include/directory/file.php"); ?>
I don't really like it, seems as an awkward way to solve it, but it seem to work whatever system php runs on and wherever the file is placed, making it system independent.
To reach files outside the web directory add some more ../ after $root, e.g. $root."../external/file.txt".