Jump to content

file_exists and fopen not working with paths, only same directory


Recommended Posts

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

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.

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.