I can't seem to get the fopen thing to work, or otherwise, I'm just stupid if it is but I don't know it.

Here is the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<HEAD>
<title>Open the file</title>
</HEAD>

<body>
 
<?php

$fp = fopen("http://www.mydomain.org/order.txt", "a+");
fclose($fp);

?>

</body>

</html>

If that should work, how would I be able to tell, or should I add something to see if it works.

Thanks

Recommended Answers

All 14 Replies

Alright, your idea (buddylee) worked, by telling me that the file did not open, but how can I get the file to open? This is the big problem that I have been having.

Do you have access to the server? Your host may have the functions disabled. If you are not sure, place the following into a new php file and name it phpinfo.php:

<?php
phpinfo();
?>

This page will output all of apaches settings. What you should look for is under PHP Core. The directive is named disable_functions. If fopen is disabled, it will be listed here. Just a thought.

Do you mean this?

Directive Local Value Master Value
allow_url_fopen On On

Or this one:

disable_functions no value no value

And since I own that website, and since I can save other files to that folder, I'm pretty sure that I have access, or does it have to do with the folder permissions thing? The permissions code for the folder that I want to open the file in is "drwxr-xr-x".

Would you want to see the actual phpinfo file?

No, it was just a hunch. Can you test a local file? Create a text file, type a couple of words in it to give it some testable content and save it as testFile.txt in the same directory as your current script. Now run the following:

<?php
$file="testFile.txt";
$fp = fopen($file, "a+");
if($fp){
echo "file opened ";
}
else{
echo "file did not open ";
}
if(filesize($file)!=0)
	{
	$contents = fread($fp, filesize($file));
	echo $contents;
	}
	else
	{
	echo "The file is empty";
	}
	fclose($fp);
?>

Note that the script checks filesize before attempting fread. You'll get an error attempting fread if the file is 0.

I believe you can only use a write mode such as 'a+' with fopen on a remote file if using ftp:// as the protocol...even so, you would have to have appropriate ftp permissions to write/append. More about that here:

http://us.php.net/manual/en/features.remote-files.php

It worked, and inside the text file I put in the words Hi, and this is what was outputted:

file opened Hi

So opening files work if it's already there, but I can't create new ones for some reason

Huh. Just to verify this, run the above script again but change the file name to something not in the current directory. If it creates a new file, it will output "file opened The file is empty"

Do you mean change the text file name to something different?

Yeah, change it to testFile2.txt or something.

Alright, I think it just worked, I changed the name of the text file, ran the php file again, and there was another text file named testFile.txt. The script outputted: file opened The file is empty

Ok, now maybe I will just have to open files the way you did. Can you explain how this part of the code works?

<?php

$file="testFile.txt";

$fp = fopen($file, "a+");

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.