Now I know that you have to FTP the files to the server but I'm more confused about the directory structure... Let me explain..
Here is the localhost directory structure
C:/wamp/www/store/{Here is the entire project}
Here is the online server directory structure
htdocs/{The project will go here}
My problem
As you can see, my root directory for the project is "/store" in the localhost environment. So I used relative path in my code to refer to the root directory which is the "/store/". You can see that I use this type of referencing throughout the project.
print "<div class='globalerror'>
<div class='globalerror-content'>
<h5>Uh-oh! There's an error</h5>
<p>You must go back</p><br/>
<p class='back'><a href='/store/'>Go back</a></p>
</div>
</div>";
If I upload the files to "htdocs" It will give me an error, which is, Directory not found. If I create a folder named "store" inside the htdocs folder... I'd have to go www.mydomain.com/store (which I don't want)
So, did I messed up pretty badly? Or is there a simple fix? Or do I have to edit all the "/store" to "/" in my project?
Simple, You have to change path of directory. Create new directory and give the path of that directory. Because there is no directory in your server.
Currently you can do modify the .htaccess file and can manage urls, change from store to root
Ref 1
Ref 2
Ref 3
The best option is to set the base url in single variable and use the variable around the project.
You can consider the below option, If you are willing to edit the all the links.
settings.php
/*
Local Server = http://localhost/store/
Online Server = http://www.example.com/
*/
$WEBSITE_URL = 'http://www.example.com/';
The php files
Find & Replace /store/ with $WEBSITE_URL , check the single/double quotes
require_once('settings.php');
print "<div class='globalerror'>
<div class='globalerror-content'>
<h5>Uh-oh! There's an error</h5>
<p>You must go back</p><br/>
<p class='back'><a href='".$WEBSITE_URL."'>Go back</a></p>
</div>
</div>";
Have you try URL rewriting with .htacess ?
URL rewriting with PHP
If it possible you could change the Folder inside Apache configuration( i presume you use Apache)
Using a directory in VirtualHost ServerName
Related
I'm making an intranet for a post-sale customer service entreprise. Employee need to be able to upload img files to the intranet's server and i need to store them in a directory with is BEFORE www (the website's root directory).
Doing this using php is pretty easy but how to include these imgs on the website once they're uploaded ?
I tried this code
<img src="../img/img.png"/>
This is not working because i can't send a file if it is OUTSIDE the server's www directory ...
Is there any proper way to do that ?
Current treeview :
server root directory
|www
|(all server files)
|img
|(all img files)
(the server's index.php is located in www and the files are in img)
You cannot directly access any file outside your web directory. As your question includes the tag PHP as well, I assume you may want to use it.
What you can do is the following:
Inside your www directory, create a "image.php" file, with a similar content to:
<?php
header('Content-Type: image/png');
readfile("../img/" . $_GET['img']);
?>
And call your images with
<img src="image.php?img=myimage.png" />
Please be aware that your PHP file shouldn't be that simple :) As you may want to address multiple image formats (and providing the correct header for them), checking for malicious file path/inclusions (you don't want to use $_GET without validating/sanitizing the input), extra caching etc. etc. etc.
But this should give you an idea on how you can target your issue.
It depends on what you are trying to accomplish and how.
With "simple" html commands it is as you found out. You can't go to a directory outside of the www root.
(for xampp applications on C for exmple it is most of the time c:\xampp\htdocs).
If you are ok with using serverside commands to accomplish what you want to achieve then you could use php to do a workaround, by reading the appropriate files in via PHP.
For example if your file is named "myimg.gif" and lies in "c:\pics"
<img SRC="data:image/gif;base64,<?php echo base64_encode(file_get_contents("c:\pics\myimg.gif"));?>">
With that you are reading the contents of the file and writing it directly into the img tag.
Be aware that you need to change image/gif to what you really need there.
You can't directly access file outside/above your root directory (www or public_html).
You can create virtual directory in Apache server configuration. Google "apache virtual directory" for more information.
Virtual directory configuration example:
<IfModule alias_module>
Alias /uploaded_images/ "/home/someuser/somewhere"
<Directory "/home/someuser/somewhere">
Allow from all
</Directory>
</IfModule>
uploaded_images directory will be available from web like normal directory in www directory. You can copy files from there to another location (kind of buffering).
You can also open/copy that file by ftp from php level without changing anything in apache, but this may be really slow (use it only if you can't control apache config, virtual directory is much better).
php.net: FTP
I Agree with #kamil's idea about creating a virtual directory. However if you want to go the php route, I wrote some code that open's images in a directory before WWW and copies them to the www/images folder.
<?php
$imagepath = "../images/";
define('IMGPATH', realpath($imagepath).DIRECTORY_SEPARATOR);
$cachewwwimg = "images/";
$imagename = 'image.jpg';
copy(IMGPATH.$imagename, $cachewwwimg.$imagename)
?>
<img src="<?php echo $cacheimg.$imagename;?>"/>
I solved this by simply creating a symbolic link pointing to the global physical folder on my hosting. I didn't have root access, so I created a simple PHP file, calling it symbolic_link.php, that i put in the root of my website and that creates the symbolic link for me. Here it is:
<?php
$target = '/home/<user>/images/'; // hosting physical directory
$link = 'images'; // symbolic link inside root of web site
unlink($link);
symlink($target, $link);
After the creation of the symbolic link, I deleted the file.
This is the hosting directory structure:
/home/<user> // hosting root
|
+--/images // hosting global dir
| |
| +--/logo.png // global image
|
+--/www.website.com // hosting website dir
|
+--/images // symbolic link
Now you can use the image as if it were in your website folder:
<img src="https://www.website.com/images/logo.png" alt="...">
Is it possible to include file from the ftp's root in a script in a subdomain ?
This doesn't work for me:
include($_SERVER['DOCUMENT_ROOT'].'/joomla.php');
Can someone help me ?
I think first you need to determine where is your subdomain files are placed and echo the $_SERVER['DOCUMENT_ROOT'] command to see the full path then you can combine two paths and make necessary adjustments.
Usually but not always, subdomain files are placed in a folder one level up then the main site's root folder so you may need to replace the folder name according to your needs in the output of server document folder function.
To give an example if your domain files are in a path like
/var/www/vhosts/example.com/httpdocs/
and if your subdomain path is like
/var/www/vhosts/example.com/subdomain/
then you will need to replace httpdocs with subdomain (or the other way around) in the output where you call $_SERVER['DOCUMENT_ROOT']
I'm making an intranet for a post-sale customer service entreprise. Employee need to be able to upload img files to the intranet's server and i need to store them in a directory with is BEFORE www (the website's root directory).
Doing this using php is pretty easy but how to include these imgs on the website once they're uploaded ?
I tried this code
<img src="../img/img.png"/>
This is not working because i can't send a file if it is OUTSIDE the server's www directory ...
Is there any proper way to do that ?
Current treeview :
server root directory
|www
|(all server files)
|img
|(all img files)
(the server's index.php is located in www and the files are in img)
You cannot directly access any file outside your web directory. As your question includes the tag PHP as well, I assume you may want to use it.
What you can do is the following:
Inside your www directory, create a "image.php" file, with a similar content to:
<?php
header('Content-Type: image/png');
readfile("../img/" . $_GET['img']);
?>
And call your images with
<img src="image.php?img=myimage.png" />
Please be aware that your PHP file shouldn't be that simple :) As you may want to address multiple image formats (and providing the correct header for them), checking for malicious file path/inclusions (you don't want to use $_GET without validating/sanitizing the input), extra caching etc. etc. etc.
But this should give you an idea on how you can target your issue.
It depends on what you are trying to accomplish and how.
With "simple" html commands it is as you found out. You can't go to a directory outside of the www root.
(for xampp applications on C for exmple it is most of the time c:\xampp\htdocs).
If you are ok with using serverside commands to accomplish what you want to achieve then you could use php to do a workaround, by reading the appropriate files in via PHP.
For example if your file is named "myimg.gif" and lies in "c:\pics"
<img SRC="data:image/gif;base64,<?php echo base64_encode(file_get_contents("c:\pics\myimg.gif"));?>">
With that you are reading the contents of the file and writing it directly into the img tag.
Be aware that you need to change image/gif to what you really need there.
You can't directly access file outside/above your root directory (www or public_html).
You can create virtual directory in Apache server configuration. Google "apache virtual directory" for more information.
Virtual directory configuration example:
<IfModule alias_module>
Alias /uploaded_images/ "/home/someuser/somewhere"
<Directory "/home/someuser/somewhere">
Allow from all
</Directory>
</IfModule>
uploaded_images directory will be available from web like normal directory in www directory. You can copy files from there to another location (kind of buffering).
You can also open/copy that file by ftp from php level without changing anything in apache, but this may be really slow (use it only if you can't control apache config, virtual directory is much better).
php.net: FTP
I Agree with #kamil's idea about creating a virtual directory. However if you want to go the php route, I wrote some code that open's images in a directory before WWW and copies them to the www/images folder.
<?php
$imagepath = "../images/";
define('IMGPATH', realpath($imagepath).DIRECTORY_SEPARATOR);
$cachewwwimg = "images/";
$imagename = 'image.jpg';
copy(IMGPATH.$imagename, $cachewwwimg.$imagename)
?>
<img src="<?php echo $cacheimg.$imagename;?>"/>
I solved this by simply creating a symbolic link pointing to the global physical folder on my hosting. I didn't have root access, so I created a simple PHP file, calling it symbolic_link.php, that i put in the root of my website and that creates the symbolic link for me. Here it is:
<?php
$target = '/home/<user>/images/'; // hosting physical directory
$link = 'images'; // symbolic link inside root of web site
unlink($link);
symlink($target, $link);
After the creation of the symbolic link, I deleted the file.
This is the hosting directory structure:
/home/<user> // hosting root
|
+--/images // hosting global dir
| |
| +--/logo.png // global image
|
+--/www.website.com // hosting website dir
|
+--/images // symbolic link
Now you can use the image as if it were in your website folder:
<img src="https://www.website.com/images/logo.png" alt="...">
Am trying to use a config file for a database and rating script but the problem is the config file is in this directory :
website.com/include/config.php aka websitename/include/config.php
The rating script needs the config and is accessed like this:
include_once("config.php");
I want the config to be in:
"/files/website/"
A directory level up from the website root folder.
I have been trying with:
"../files/website/" and other variations but can not figure out how to link them.
I have managed to put one config file and access it, but with this ajax rating script the only way for it to work is to have the config in the /include/ folder next to:
rating_process.php - has this link : include("inc/config.php");
rating_functions.php - has this link : include_once("config.php");
rating_total_functions.php - has this link : include("inc/config.php");
Hope i've explained myself here
Right, looking at my hosting now:
$_SERVER['DOCUMENT_ROOT']; outputs this: /foldy/homepages/11/username/htdocs/webbysite
My index file is located at: /foldy/homepages/11/username/htdocs/webbysite/index.php
The included rating script is located in: /foldy/homepages/11/username/htdocs/webbysite/include/
I want the config to be in /foldy/homepages/11/username/htdocs/secretfiles/config.php
Am trying to some how go out of: webbysite folder and then into secretfiles (sibling folders)
I have tried adding ../ and so on, but am missing something obviously :(
Try
$configLocation = $_SERVER['DOCUMENT_ROOT'].'../files/website/config.php';
include_once($configLocation)
but the problem is the config file is in this directory
it is not a problem at all.
just keep it as is.
Concerning your particular problem, your problem is that you don't know where you want to put your file. /files/website/ is not likely a right path and it is apparently not one level high from webroot.
So, first of all make your mind about the right path to the directory and it's relative position to the web root
if you are concerned about security ( because your config file contains the db details ) i would place the db config file outside the site root folder and then require_once('../../dbConfig.php') from the script that's creating xml or json for your ajax
more exactly ...
your site folder might be here: /var/www/html
set a virtual host (done differently on Linux and Windows) and point your domain to a sub folder inside /html so that the new path to the site root is /var/www/html/site.
then place your config file in /var/www/html and call it from your scripts inside your /site folder using require_once('../dbConfig.php)`.
your db details are outside the site folder
i have my web folder in c:/wamp/www/ which is the default webroot for wamp. Then i have a folder image in c:/image/ that contains images been processed by another application which i wont like to relocate. I want to be able to load up an image with its file name from c:/image/
e.g. img src = "c:/image/FA12.jpg"
within my PHP scripts. I really Need Help on this Please. Thanks for your support.
If you need to read a file into php use "c:\image\FA12.jpg". You may also create a link "file://c\image\FA12.jpg". There's no way to create a relative path, since it is outside root dir.
As per my knowledge it is better to create a folder of your images under your localhost or server ('var/www/images') to have a proper relative URL to that images.
Url's like c:\image\fa12.jpg can create problems when you make your product online. Other wise you can do provide static urls as kirilloid suggest or you can copy that images to some proper folder in your localhost sever with use of php function "copy" and use that folder as a reference. avoid static url's if possible.:)
as i have linux system now i tested below code locally. create one file test.php under /www and put below code in it and run file in browser. see if this can be your solution
<?php $link = $_SERVER['DOCUMENT_ROOT'];
$domain = strstr($link, '/www');
echo $domain;//this will output /www
$root = strstr($link, '/www', true);
echo "<br>".$root;//this will output /var
?>
You can try this by putting "/wamp/www" instead of "/www" in above code and you will get c:/ (perhaps) in $root and then appeng folder name images and path accordingly to $root.
May this will help you