PHP/Linux Readfile Path Using Symlink - php

I want to move my data directory outside of the web root. I have file paths stored in my database and would like to be able to redirect looking for files through a symlink instead of having to change the saved paths.
Example, currently I have a structure like this:
/root/webroot/data/file.ext
And my database path will be: /root/webroot/data/file.ext as well. I'd like to move data outside of webroot so I have:
/root/data/file.ext
/root/webroot/data -> root/data
Where /root/webroot/data is a linux style symlink to root/data. I have set this up, but when I do a readfile on the previously stored path (/root/webroot/data/file.ext) it fails. I.e. I do this:
readfile('/root/webroot/data/file.ext');
I get back:
Warning: readfile(/root/webroot/data/file.ext) [function.readfile]: failed to open stream: No such file or directory in [file] on line ...
Is there anything I can do to help these paths resolve?
Note: The permissions of the folders and files all match. That of the symlink is root:root, from which it was created, but chown doesn't seem to work on it.

Where are you running the script from? If you are calling the directory without the leading slash:
readfile('root/webroot/data/file.ext');
then you better make sure it is running in the same directory root is sitting in. However, if you are running this from webroot, you will need to update the readfile path to something like:
readfile('data/file.ext');

Related

Mysql is not connecting with php in wamp server.

require(../connect_db.php): failed to open stream: No such file or directory in C:\wamp\www\php\require.php on line 10 in wamp server. This is my first time trying to connect to mysql with php. Can anyone help me please?
You are using a relative path (../connect_db.php).
You need to take into consideration that the path is not relative to the file where the require is used, but to the file that is run.
So, if you have your main file index.php, you could have the following:
// index.php
include('config/config.php');
// config/config.php
include('config/db/connect.php'); // relative to index.php folder
include('db/connect.php') // WON'T WORK
The directory structure would be:
index.php
config/
config.php
db/
connect.php
Be sure that connect_db.php file exists in C:\wamp\www dir.
Also check folder permissions on that directory (I don't know which user uses wamp, but try first to give permissions to all users and then filter a little more).
be sure to check these following reasons:
file exists
file location is correct ( '../file') means upper location
failed to open stream: No such file or directory
Your path is incorrect.
For example, your code require('../connect_db.php'); does the following;
Go up one directory
Require connect_db.php
If we have the following tree structure, your code will work
/wamp/
www/
connect_db.php
php/
require.php
I would suggest moving connect_db.php into php/ and altering your require to become require('connect_db.php');. Or, paste your tree structure, and we can see where you're going wrong.

PHP move files directory

I am trying to move my jpg image file from one directory to another using the rename() function. However it keeps on giving the error saying No such file or directory. I have changed it to the copy() function with the following error failed to open stream: No such file or directory.
I am trying this as following:
rename('/_upload/1.image.jpg', '/_accepted/test/1.image.jpg');
The file is orginally already in my htdocs/_upload folder. This PHP file is already in my htdocs folder. All permissions are set to 777 but giving the same error.
Instead of starting / in your path, use ./:
rename('./_upload/1.image.jpg', './_accepted/test/1.image.jpg');
/ means that you are giving path starting the server's root directory.
./ starts with your current directory.
In your example, the arguments to rename are file names. /_upload/1.image.jpg is an absolute file name. That means, it is relative to the root directory. It is not relative to your server root, document root or current dierctory.
try this to give complete abosolute path
rename($_SERVER['DOCUMENT_ROOT'].'/_upload/1.image.jpg', $_SERVER['DOCUMENT_ROOT'].'/_accepted/test/1.image.jpg');

cannot directly call the php file inside folders using the directory path

I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.

Reverting to a different file when PHP fails to find a specific file (include function)

Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.

Find filepath to public_html directory or it's equivalent using PHP

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.

Categories