Cultureshock Posted February 27, 2010 Share Posted February 27, 2010 I have a subdomain with two folders. For some reason I can't check if a file exists from "directory A" from "directory B," nor can I create a file in "directory A" from "directory B," however I CAN create a file in directory B into directory A. For whatever reason the path doesn't work. $newfile = "/inc/example.php"; if(file_exists($newfile)){ echo "yes <br>".$newfile; }else{ $createfile = fopen($newfile, 'w') or die("can't open file"); fclose($dreatefile); }; It gives me this warning: Warning: fopen(/example.php) [function.fopen]: failed to open stream: Permission denied in /home/MYSITE/public_html/subdomain/folderA/create.php on line 20 can't open file Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/ Share on other sites More sharing options...
ialsoagree Posted February 27, 2010 Share Posted February 27, 2010 Permission denied means you don't have permission to access the file. It's not a PHP problem, the web user (most likely a guest) doesn't have permission to access the specified file. Access to create files in a directory is totally unrelated to access to read/write on an existing file in the same directory. The difference is that the user has access to the directory, but NOT to the file within the directory. Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/#findComment-1019095 Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 A path for fopen() beginning with a / will be seen as the root of the filesystem, not the webroot. So, either give it the full path from the root of the filesystem, or try a relative path from the script location. Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/#findComment-1019096 Share on other sites More sharing options...
Cultureshock Posted February 27, 2010 Author Share Posted February 27, 2010 Thanks guys for the fast replies - A path for fopen() beginning with a / will be seen as the root of the filesystem, not the webroot. So, either give it the full path from the root of the filesystem, or try a relative path from the script location. How do I do a relative path into another folder at the same level, then? I need this code to be able to work on any subdomain/ domain so that I can reuse it without editing the code, so I can't do a full path. Permission denied means you don't have permission to access the file. It's not a PHP problem, the web user (most likely a guest) doesn't have permission to access the specified file. Access to create files in a directory is totally unrelated to access to read/write on an existing file in the same directory. The difference is that the user has access to the directory, but NOT to the file within the directory. I'm assuming it's a path problem because the access is being denied into the same exact page. Thanks though. Besides, fopen() is how you create files that don't exist, right? Or is the internet lying to me I wouldn't be surprised. haha Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/#findComment-1019100 Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 if you are in directory a and directory b is alongside, then you can fopen('../b/yourfilename'); Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/#findComment-1019105 Share on other sites More sharing options...
Cultureshock Posted February 27, 2010 Author Share Posted February 27, 2010 if you are in directory a and directory b is alongside, then you can fopen('../b/yourfilename'); hmph. ... I knew that. (/ is the web root in headers and includes... I guess I assumed it would be the same. haha) Anyways, enough blabbering. It worked, obviously. Thanks, and thanks for the quick reply once again. Eventually I'll stop being such a novice. Quote Link to comment https://forums.phpfreaks.com/topic/193592-file_exists-and-fopen-not-working-with-paths-only-same-directory/#findComment-1019110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.