/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] File exists, but can’t open it.

I don’t understand why the following code does not work. Can someone tell me what I did wrong?

The results of this code is shown below and indicates the file exists, but I can’t open the file.

This is similar to my previous post where I thought it was a permission problem when I was trying to write, but this is only a read request. Are files not read by default?

[CODE]
<?php
$filename = “..\Text\Text.txt”;

if (file_exists($filename)) {
echo “The file $filename exists”;
} else {
echo “The file $filename does not exist”;
}

$handle = fopen($filename, “r”)or die(“<br> can’t open file”);
fclose($handle);
?>
[/CODE]

Here is the results of running this code,

[QUOTE]

The file ..TextText.txt exists
can’t open file

[/QUOTE]

to post a comment
PHP

33 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 22.2010 — Check the file permissions. I'm not sure, but if you have read/execute on the directory, you may be able to "see" the file but unable to open it if you do not have read permission on the file itself -- and remember that in many installations your PHP script is run by the Apache user, not your login account user.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Check the file permissions. I'm not sure, but if you have read/execute on the directory, you may be able to "see" the file but unable to open it if you do not have read permission on the file itself -- and remember that in many installations your PHP script is run by the Apache user, not your login account user.[/QUOTE]

I have logged on to the user account for this website and set the permissions for the Text folder. which contains the file Text.txt file, to read and write. I have set the subdirectories to inherit.

There is no way to set permissions for individual files.

I have been in contact with godaddy.com via email, and after a number of email exchanges, they do not have a solution for this problem.

What more can I do? Do I just have to give up using any file read or write code on this server.
Copy linkTweet thisAlerts:
@NogDogNov 22.2010 — Just for the halibut, is there any difference if you set the file variable using forward slashes (and let PHP figure it out based on the OS)?
[code=php]
$filename = "../Text/Text.txt";
[/code]

Also, you might want to make sure all errors are displayed for now, in case it's something like the open_basedir setting or such.
[code=php]
<?php
//top of script:
ini_set('display_errors', 1); // set to 0 for production version
error_reporting(E_ALL);
[/code]
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Thanks for your help.

Yes, I did try the forward slashes and got the same results.

I also put the file in the same directory as my php file and got the same results.

I will add the error code you mention and try it again.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — I just added the code you suggested and ran the file, which did not report any errors, yet I am still getting the "can't open file" error from my code.

I am also trying to open a file that is in the same folder as the php file, to eliminate the possible problem with file path.

The temp.txt files contains only this text: "This is a temp file."

Here is my present code:

[CODE]
<?php
ini_set('display_errors', 1); // set to 0 for production version
error_reporting(E_ALL);

$filename = "temp.txt";

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

$handle = fopen($filename, "w") or die("<br> can't open file");
fclose($handle);
?>
[/CODE]
Copy linkTweet thisAlerts:
@NogDogNov 22.2010 — Hmm...wish I knew what to say. I tried your script on my PC and did not get the die() message (setting $filename to a file in the same directory). ?
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Hmm...wish I knew what to say. I tried your script on my PC and did not get the die() message (setting $filename to a file in the same directory). ?[/QUOTE]

Thank you. I guess I will just give up on developing a CM system for this website.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — It really looks like a permissions problem. If I had to guess the user running the script (generally www-data, or apache) has read permission on the file but not write permission.
Copy linkTweet thisAlerts:
@dfordNov 22.2010 — I work with oscommerce alot and have heard of users having problems with godaddy servers in many strange ways. Are you able to set your permissions to 777? just to test if it is in fact a permission issue or does godaddy not let you into the chmod?
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — chmod does not work at all on this server.

I went in through the user account and was able to set the folder to read and write, but there is no way to set the permission for the file. The godaddy technical support say there is nothing they can do because I am using php, and they won't even look at the php code.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — Write a small script that writes the file in the first place. Then it will have write permissions for the "owner" (creator) user.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — It really looks like a permissions problem. If I had to guess the user running the script (generally www-data, or apache) has read permission on the file but not write permission.[/QUOTE]

The php code indicates the file exists, but it will not either read or write form/to the file.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — The php code indicates the file exists, but it will not either read or write form/to the file.[/QUOTE]

The code you've posted is trying to open the file for writing. If you switch the "w" to "r" does it still behave the same way? If so, perhaps the fopen function has had restrictions at the host level?
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — The code you've posted is trying to open the file for writing. If you switch the "w" to "r" does it still behave the same way? If so, perhaps the fopen function has had restrictions at the host level?[/QUOTE]

Yes, I have tried both "w" and "r", and neither works. There are no provisions for changing the file permissions in the User Account File Management page. Numerous phone calls and email messages with godaddy were all for nothing. They say there is nothing they can do to fix this problem.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Write a small script that writes the file in the first place. Then it will have write permissions for the "owner" (creator) user.[/QUOTE]

Unfortunately chmod does not work on this server.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — Unfortunately chmod does not work on this server.[/QUOTE]

Write your PHP script to create the file initially...you shouldn't need chmod for files created by the php user.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — I have been working on this problem for almost 3 days, and have tried everything I can think of. During this time I have read through many different forums, and it appears I am not alone with this problem. Also, with all the reading I have done, no one has come up with a solution that works for this server.

I just don't see how a hosting service provider can host a site where you can not read and write files.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Write your PHP script to create the file initially...you shouldn't need chmod for files created by the php user.[/QUOTE]

OK, I am a relatively new php user, and have not yet found a way to create a file with php. I have both created a file and uploaded it, and I have created a file through the user interface through logging on to the account, but neither of these worked.

I have deleted the file I uploaded and then tried writing a file by using this statement:
[CODE]$fh=fopen($myFile,"w")or die("can't open file");[/CODE]

I understand should create the file if it does not exist. Is there another way to create a file with php which assigns permissions? I am open to all suggestions.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — Try running something like this for a file that does not exist yet and make sure you are targeting a folder that has write permissions.
[code=php]
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]

See if that will run for you.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Try running something like this for a file that does not exist yet and make sure you are targeting a folder that has write permissions.
[code=php]
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]

See if that will run for you.[/QUOTE]


I received "can't open file" message. I tried to write it to my "Text" folder which I have set the permissions to read and write.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — First question...windows or linux hosting?

Second question...can you take a screen shot of your file browser showing the directory that has "write" permissions?

http://help.godaddy.com/article/2535#advanced

http://www.kntreed.com/post/2008/02/Changing-File-Permissions-with-Godaddy-Hosting.aspx

http://aackose.wordpress.com/2010/03/14/setting-write-permission-to-app_data-for-a-godaddy-hosted-site/
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — First question...windows or linux hosting?

Second question...can you take a screen shot of your file browser showing the directory that has "write" permissions?

http://help.godaddy.com/article/2535#advanced

http://www.kntreed.com/post/2008/02/Changing-File-Permissions-with-Godaddy-Hosting.aspx

http://aackose.wordpress.com/2010/03/14/setting-write-permission-to-app_data-for-a-godaddy-hosted-site/[/QUOTE]


OS/Hosting Type: Windows/Shared Hosting

Hosting Configuration:

.Net Runtime Version: ASP.Net 1.1

IIS Version: IIS 6.0

Here is a screen capture of the directory from my FTP program,

http://www.blackcanyonsystems.com/4-3/dir.jpg
Copy linkTweet thisAlerts:
@dfordNov 22.2010 — ya, I was always leery of any website company who sells to the masses by paying crap tons to female models and Superbowl commercials, if they were smart they want a spokeswomen like Pauley Perrette, and then actually offer a service comparable to other host. Not to get off the subject but I would be baffled if it wasn't a permissions problem, that they seem not to care to support.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — You'll need to capture what the control panel file manager says not the ftp program. The ftp program is not showing the data we would need to see (notice all the "?????"?). Most of your problem could be avoided by using an appropriate host for PHP (not IIS/windows as they never do mix very well for production environments).
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — ya, I was always leery of any website company who sells to the masses by paying crap tons to female models and Superbowl commercials, if they were smart they want a spokeswomen like Pauley Perrette, and then actually offer a service comparable to other host. Not to get off the subject but I would be baffled if it wasn't a permissions problem, that they seem not to care to support.[/QUOTE]

This is the first time I have run into a problem with this hosting service with all the websites I have designed. In fact I have been promoting them to my clients.

This is also the first time I have tried to write directly to a file, and I am very disappointed with their responses from their technical service people. I guess I will have to look for another hosting service that does provide what I need along with good technical service.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — http://help.godaddy.com/article/2323[/QUOTE]

I have read this, but there is no way to set permissions for the file. You can see I have them set for the folder.

http://www.blackcanyonsystems.com/4-3/dir2.jpg

http://www.blackcanyonsystems.com/4-3/dir3.jpg
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — Did you try the code block from above?
[code=php]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Did you try the code block from above?
[code=php]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
[/QUOTE]


I have to leave for a few hours, but will do this when I return.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Did you try the code block from above?
[code=php]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
[/QUOTE]


OK, I had time to run this before I leave, and I got the same "can't open file" message.
Copy linkTweet thisAlerts:
@criterion9Nov 22.2010 — Have you unchecked the "inherit" checkbox? I saw some other people with a similar problem that said telling the directory not to inherit permissions from parent directories fixed the problem.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 22.2010 — Have you unchecked the "inherit" checkbox? I saw some other people with a similar problem that said telling the directory not to inherit permissions from parent directories fixed the problem.[/QUOTE]

What I noticed is that I can check this checkbox and click OK. The next time I look at permissions, the box is unchecked. I just tried this about 6 times and the same thing happens.

Just as a double check, I made sure the box was unchecked and then I clicked OK. I then ran your latest code and received the same reply as before.
Copy linkTweet thisAlerts:
@lkeeneyauthorNov 25.2010 — I found the server will allow me to read and write the file with asp code.

I want to thank everyone for their help on this problem.
×

Success!

Help @lkeeney spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...