PHP - Access Files Placed Outside Of The Site Root?
I have some files saved outside the webroot and need to allow posts in WordPress to be able to access the files.
Following is the location of one of the files:
/home1/Mathone/TESTS/Test1/index1.html
I created a Download.php file with the following code and saved it to public_html of my site:
<?php
$path = '/home1/Mathone/TESTS/'. $_GET['filename']; $mm_type="application/octet-stream"; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: " . $mm_type); header("Content-Length: " .(string)(filesize($path)) ); header('Content-Disposition: attachment; filename="'.basename($path).'"'); header("Content-Transfer-Encoding: binary\n"); readfile($path); // outputs the content of the file exit(); ?> Next, I created a post in wordpress with the following link: <a href="www.mysite.com/download.php?filename=Test1/index1.html">download</a> I am getting an error This is somewhat embarrassing, isn’t it? It seems we can’t find what you’re looking for. Perhaps searching can help. Can anyone tell me what I am doing wrong? Thank you Similar TutorialsI having been searching through the forum and other websites for information on reading a file (an image file) located above my root directory. I would like to store images outside the root directory and then use PHP to call those images for registered users. Basically I want to create a secure web gallery. I am using GoDaddy Deluxe shared hosting, and have managed to create a webroot directory and place files above it. I have found several scripts for reading files using the readfile() function, however when I actually implement the script to find the file and read its contents, it does not seem to find the file. I know my directory path is correct, as I can call an echo function inside a test.php file located in the same folder as the images (above my root). I would love some incite as to where I went wrong, or if there is a specific GoDaddy work around that needs to be implemented. Also, if I am going about this the wrong way, please let me know if there is a more effective way of going about this! Thank you so much! I have around 14 script files for an admin page.
I am looking to block the access to 10 of them with .htaccess, so they can not get called up through the URL with the web browser, by for example guessing the name.
<Files index.php> Order Allow,Deny Allow from all </Files>How to set up, so I can have "those" 10 files in there? Also, when comes to having an admin page and blocking the access to "those" script files is this the proper way to do it? I would appreciate the suggestions a lot! Edited by glassfish, 25 October 2014 - 11:06 AM. Hi, i am learning WordPress and have just started, but i have a problem....when i try to access my site i get this: Warning: Cannot modify header information - headers already sent by (output started at /home/smra9/public_html/wp-content/themes/evanescence/functions.php:6) in /home/smra9/public_html/wp-includes/pluggable.php on line 890 when i open pluggable.php and i go to the line 890 it says: header("Location: $location", true, $status); i will also copy a text from line 885 to 890 so maybe you will understand better: if ( $is_IIS ) { header("Refresh: 0;url=$location"); } else { if ( php_sapi_name() != 'cgi-fcgi' ) status_header($status); // This causes problems on IIS and some FastCGI setups header("Location: $location", true, $status); } Can anyone help me, i don't know what to do Hi, how much bandwidth is used for 1000 downloads per day or for each download? Is it safe to have no download limits? Thanks
I'm making an admin so I need to be able to do this: https://admin.mysite.com Run an upload image script from a file, saves image to folder in http://www.mysite.com Image Folder Having problems pointing to where files are. Is this the root?: Code: [Select] include "../connectdb.php"; That file is locate din the root. If I am already in the root when I include that statment, the file isn't found. If I use the above code while I am in the subdirectory hello, (root/hello), it works. I use this in every page, so I think I need a way to point to the absolute root, not relative. Hi guys, I've got a simple script that I'm running from a folder above the root (as a cron job). I've tried the script in the httpdocs folder and it works fine. Code: [Select] mail('email@example.com', 'Before include', '', 'From:<email@example.com>'); // include the myriad class include($_SERVER['DOCUMENT_ROOT'].'/path/to/class'); mail('email@example.com', 'After include', '', 'From:<email@example.com>'); // initiate and instance of our class $obj = new class_name; $email = $obj->email; $file = $obj->file_name; mail('email@example.com', 'End of file', $email.' & '.$file, 'From:<email@example.com>'); If I run this file in httpdocs, it's fine and I get all emails. If I run this from 1 folder above the root (from a cron job), I get the first 2 emails but not the last one, suggesting that the include hasn't worked. Could this be a permissions problem? I have tried running the cron job as both root and apache and changed the owner/group of the script to match. Is there a reason why includes won't work above the root? If I an deep inside of nested folders, how do I get back to the Web Root (as far as directory notation goes)?? Debbie Hi All, I googled this and there is endless results. I went through a lot of them but couldn't get this working properly. How do I link from within my web site root to files outside the root? It works for me using relative links i.e. ../../phpfiles/includes but that is going to get messy and I can't get a way of doing absolute links to work. If someone could lay that out so a newbie can get it clearly I would really appreciate it! Also - I understand why I should put all of my php files outside the web root but is this a guaranteed way to secure these files other than someone hacking my ftp access? I've looked at a few site hierarchy examples - Am I right that the only pages within the web site root should be template pages with calls to required files (outside the root), session checks, and content includes and all other includes that have php executable code should be outside the root? I really appreciate the advice and insight. Thank you! Hi, I want to get root path and use on links even if i'm in sub to sub folder etc... suppose my site name is fitness.com and having two subfolder. so url will become http://www.fitness.com/dir/dir_sub here i want to put a link to go to root directory file. there are different ways to do that. e.g: Code: [Select] <a href='../../filename.php'>go to that page</a> but i want to use some constant that always shows to root directory. as i defined here Code: [Select] define("SERVER_NAME" , $_SERVER['HTTP_HOST']); //and used it like this <a href='<?php echo SERVER_NAME; ?>/filename.php'>go to that page</a> it works when we're on root directory. but when we go to sub directory it added sub directory name with it. which i don't want. is there any way? Thanks is there a way in php to link from the root dir ? like in html you just use the '/' at the start for the link " <a herf="/link.php" ></a> " but i noticed this does not work when using php like include or require. so is there anywey to tell a link to start from root dir? without using the ../../link.php Hi, I have a dynamic variable like this: /def/g/qaz/pol/cxz/cba/abc I only wish to keep this: /def/g/qaz/pol/cxz How would I do this? Thanks, - mme hey guys im still having a issue with using the root path when requiring external files. So i can use one path and never have to worry about this issue. require("/functions/function_battle.php"); the file is located here Code: [Select] C:\Software\XAMPP\xampp\htdocs\System_Lords\functions\function_battle.php i dont understand (include_path='.;C:\Software\XAMPP\xampp\php\PEAR') im suppose to be including the php folder or something? Code: [Select] Fatal error: require() [function.require]: Failed opening required '/functions/function_battle.php' (include_path='.;C:\Software\XAMPP\xampp\php\PEAR') in C:\Software\XAMPP\xampp\htdocs\System_Lords\include\battle.php on line 2 I have been getting a lot more client requests to protect files. What is the easiest way to do this. So, basically I have tried doing it outside the public directory. There are too many things that cause issues with this. I haven't been able to get a successfull implementation of this since I started working with this. So I was thinking instead about password protecting a directory that is inside public view, but still get files via PHP. Is there a way to setup a password protected directory, then retreive stuff from that directory using PHP. Or, a good way to put them outside the public folder. Everything I have tried to do to get a file to save outside of public view, has not worked. It always says uploaded but the file is never there. Also, I have verified correct permission for this as well. Hey guys. Basically i have a file containing Database connection information stored at the root of my site www.worldwidelighthouses.com/Lighthouses-Database-Connection-Information.php I want to be able to connect to it using pages such as www.worldwidelighthouses.com/Lighthouses/English-Lighthouses/Beachy-Head-Lighthouse.php and www.worldwidelighthouses.com/Lightships/Lightship1.php Obviously 1 is 1 directory down, one is 2 directories down. My server doesnt allow for include 'www.worldwidelighthouses.com/Lighthouses-Database-Connection-Information.php' So whats the best way to always find this file, no matter what directory i am in? Finding out how far away i am from the server root and adding ../ to the include for each folder down was my first idea. Is this best? Thanks, Danny. Hey y'all.
I'll gladly admit that I'm not a server guy - I have the utmost respect for people comfortable enough with *nix to handle Apache setup and configuration in a production environment. I can noodle about in *nix typically without killing anything vitally important, but trust me when I say no-one wants me to be a server administrator. That having been said, I do have a development server behind my home network firewall running Ubuntu that I use to test and develop the sites that I create on my separate Windows box. So I try to keep up with the way a shared server would be set up in a real-world situation, to a degree. For instance, I haven't bothered setting up the mail server or FTP, but I do create virtual host files for each of my projects.
So, I've got a virtual host set up on the directory /var/www/myAwesomeSite.com/ with the .conf file containing the following configuration:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName myAwesomeSite.com DocumentRoot /var/www/myAwesomeSite.com/www <Directory /var/www/myAwesomeSite.com/www> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>Now, in my php scripts echoing out $_SERVER['DOCUMENT_ROOT'] gives me 'myAwesomeSite/www' as I would expect. And using $_SERVER['DOCUMENT_ROOT'] in any require() statements actually does include the requested file, so that part seems to be working fine. My question is this: when I use a leading slash ('/'), shouldn't that equate to the document root? I use it at work all the time and it works flawlessly, but on the dev server I have set up here, it's a no-go. Is there another config command that I need to issue to make it work, or am I crazy? Basically, am I missing a setting somewhere that will make this: require_once('/path/to/my/include.php');work like this? require_once($_SERVER['DOCUMENT_ROOT'].'/to/my/include.php');*edit* I just realized that I used the wrong slash in the title of this post, but I don't see a way to change the topic title. crap. Edited by maxxd, 23 November 2014 - 11:20 AM. Hi Guys I am struggling with a shell_exec command in the long run, but I am trying to work down the root cause. The error I am getting with shell_exec is a file not found error, so I thought I would start with getting to the right folder. I have this Code: [Select] $sym_dir = "/root/"; $cwd = getcwd(); echo getcwd()."\n"; chdir ($sym_dir); echo getcwd()."\n"; Which in theory should 1st display '/var/www' as it does and then display the contents of root, at least that is my understanding! However my return is this /var/www /var/www Why cannot I move outside the web root? Cheers Dave Because I have include files in a subdirectory called "incl", I cannot rely on using relative paths because that include file is in a header on and the pages are in multiple subdirectories (or in the root). I need some way to get the location of the root (the one where my index file is located). I want to use something like this: $path= $_SERVER['DOCUMENT_ROOT']; include $path."/subdirectory/file.php"; However, when I use $_SERVER['DOCUMENT_ROOT'] I get : /home/content/j/p/f/name55/html/ which is not where the index file sits. Any ideas on how to get the directory where my index file sits? (i.e. the index that gets read when you got to www.domain.com) Hi guys, I am trying to retrieve some information from sessions. I have this in my header.php <?php if(isset($_SESSION['username'])){ $username = $_SESSION['username']; echo $username; } ?> Hello! I was working on a simple url generator, but than i though to myself, why not put everything in the root. I've heard thats not a good idea, but i'm ot sure why. I can immagine it gets really messy when you have a lot of files, but its also easy, if you ask me. I was just wondering what your view on this is. What are the pros and what are the cons? Thanks |