Free Web Hosting Forum
(#1 (permalink))
Old
Junior Member
JerielLandingin is on a distinguished road
 
Posts: 8
Join Date: Jul 2012
Default Error on fopen - 07-26-2012, 06:52 AM

Hi, I've searched the forum for this error that I'm having, and unfortunately other posts are in different languages.

The error that I'm having is this:

Warning: fopen() [function.fopen]: open_basedir restriction in effect. File(/usr/local/apache/htdocs../files/Assignment_No1.pdf) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a5970550/public_html/admin/downloadFile.php on line 7

Warning: fopen(/usr/local/apache/htdocs../files/Assignment_No1.pdf) [function.fopen]: failed to open stream: Operation not permitted in /home/a5970550/public_html/admin/downloadFile.php on line 7

Warning: fclose(): supplied argument is not a valid stream resource in /home/a5970550/public_html/admin/downloadFile.php on line 38


This is the PHP file where the error is coming from:
Quote:
<?php

$path = $_SERVER['DOCUMENT_ROOT']."../files/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['file'];

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;

case "doc":
header("Content-type: application/msword"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;

case "jpg":
header("Content-type: application/jpeg"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;

default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
?>
What I'm try to do here is to download a file that has been stored to a certain folder in the server wherein the filename is stored in the database.

What can I do to fix this error?

Similar error is being shown whenever I upload and unlink an image..

Warning: unlink() [function.unlink]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/registeredUser/userPhoto/Linkin_Park_033.jpg) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a5970550/public_html/registeredUser/viewNominee.php on line 20

Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/registeredUser/userPhoto/9X-Shiny-Silver-425x425.jpg) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a5970550/public_html/registeredUser/viewNominee.php on line 33

Please help.. Thanks

Last edited by JerielLandingin; 07-26-2012 at 08:02 AM.
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 762
Join Date: Dec 2010
Default 07-26-2012, 09:35 AM

The error is caused because the document root for hosting server and individual users is not the same.

$_SERVER['DOCUMENT_ROOT'] = /usr/local/apache/htdocs // document root of 000webhost.com server

dirname(__FILE__) = /home/a5970550/public_html // document root of a user (a5970550)

So, your code below needs to be changed as follows:

From: $path = $_SERVER['DOCUMENT_ROOT']."../files/"; // change the path to fit your websites document structure

To: $path = dirname(__FILE__)."../files/"; // change the path to fit your websites document structure

Last edited by grace1004; 07-26-2012 at 09:41 AM.
Reply With Quote
(#3 (permalink))
Old
Junior Member
JerielLandingin is on a distinguished road
 
Posts: 8
Join Date: Jul 2012
Default 07-26-2012, 10:08 AM

@grace1004, there still seem to be an error. This is the warning message that shows:

Warning: fopen(/home/a5970550/public_html/registeredUser/files/1-2.JPG) [function.fopen]: failed to open stream: No such file or directory in /home/a5970550/public_html/registeredUser/downloadFile.php on line 7

the "files" and "registeredUser" folder are under the same directory "public_html", the downloadFile.php is under the registeredUser.. How am I gonna manage this?
Reply With Quote
(#4 (permalink))
Old
Junior Member
JerielLandingin is on a distinguished road
 
Posts: 8
Join Date: Jul 2012
Default 07-26-2012, 10:12 AM

Oh, I've solved it.. hehe.. Thanks anyway..
Reply With Quote
Reply

Tags
fopen, open_basedir

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com