Problem with fopen

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6902 Views - Last Post: 05 October 2010 - 10:20 PM Rate Topic: -----

#1 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Problem with fopen

Posted 01 October 2010 - 08:56 PM

Hi all,
I have been working on this very simple script for about 2 days. This is the first time I'm writing to a file using PHP, I normally link to a database and have had no problems. What I want this program to do is just open the file and write 2 pieces of data to it and close it. It is supposed to delete anything that is already in the file.
 <?php
		if($_POST[submit]){
			$file = $_POST[type];
			$date = $_POST[date];
			$numbers = $_POST[numbers];
			$pot = $_POST[amount];
			$myfile = "../infofiles/";
			if($file == "TwooniePot"){
			//open TwooniePot.txt	
			$myfile .= "twooniepot.txt";
			}
			$data = "$date \n";
			$data .= "$numbers \n";
			$data .= "$pot \n";
			echo $myfile;

			$fh = fopen($myfile, 'w') or die("can't open file");
			fwrite($fh, $data);
			fclose($fh);	
			echo "$myfile was updated.";
		}
		?>

The reason I have the if statement in there for the file name is that there will be multiple files it could write to, I just deleted all the elseif's.
Any pointers would be great. I have checked both the file containing the code and the file to be written to (and their respective folders) that they all have file permissions set to 755. Thank you very much for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with fopen

#2 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 01 October 2010 - 09:01 PM

The output I receive is the following:

../infofiles/twooniepot.txt
Warning: fopen(../infofiles/twooniepot.txt) [function.fopen]: failed to open stream: Permission denied in /home/shopinbo/public_html/rutherford/admin/index.php on line 57
can't open file
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Problem with fopen

Posted 01 October 2010 - 10:10 PM

That means your permissions are set in such a way that your file cannot be opened by PHP. You should set the permissions on the file to 0755.
Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Problem with fopen

Posted 02 October 2010 - 02:42 AM

If your server's version of PHP supports it, make life easier on yourself and use file_put_contents as opposed to all of those functions. Not forgetting to take into account what Valek mentioned.
Was This Post Helpful? 0
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Problem with fopen

Posted 02 October 2010 - 09:20 AM

chmod("file_you_are_editing.txt", 0777);


And at the end, right before you save it, put it back to something like 0644. Just for security reasons.
Was This Post Helpful? 0
  • +
  • -

#6 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 03:04 PM

Thank you all for trying to help. Unfortunately I have tried your suggestions and I'm still gettting the following errors:
Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 60

Warning: file_put_contents(../infofiles/twooniepot.txt) [function.file-put-contents]: failed to open stream: Permission denied in /home/shopinbo/public_html/rutherford/admin/index.php on line 61

Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 62

This is how I changed the code (I've included all the ifs and elseifs this time just in case there's a problem there I've missed:
 <?php
		if($_POST[submit]){
			$file = $_POST[type];
			$date = $_POST[date];
			$numbers = $_POST[numbers];
			$pot = $_POST[amount];
			$myfile = "../infofiles/";
			if($file == "TwooniePot"){
			//open TwooniePot.txt	
			$myfile .= "twooniepot.txt";
			}elseif($file == "TwooniePotPlus"){
			//open TwooniePotPlus.txt	
			$myfile .= "twooniepotplus.txt";
			}elseif($file == "SuperJackpot"){
			//open SuperJackpot.txt
			$myfile .= "superjackpot.txt";
			}elseif($file == "YellowStar"){
			//open YellowStar.txt	
			$myfile .= "yellowstar.txt";
			}elseif($file == "PlayersProgressive"){
			//open PlayersProgressive.txt	
			$myfile .= "players.txt";
		    }elseif($file == "Bonanza"){
			//open Bonanza.txt	
			$myfile .= "bigbuck.txt";
		    }elseif($file == "AllNothing"){
			//open AllNothing.txt	
			$myfile .= "all.txt";
	    	}else{
			//open olivestar.txt
			$myfile .= "olivestar.txt";
		    }
			$data = "$date \n";
			$data .= "$numbers \n";
			$data .= "$pot \n";
			echo $myfile;

			//$fh = fopen($myfile, 'w') or die("can't open file");
			//fwrite($fh, $data);
			//fclose($fh);	
			chmod($myfile, 0755);
			file_put_contents($myfile,$data);
			chmod($myfile, 0777);
			echo phpinfo();
			echo "$myfile was updated.";
		}
		?>

Was This Post Helpful? 0
  • +
  • -

#7 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Problem with fopen

Posted 02 October 2010 - 03:18 PM

Must be the actual file that is doing the writing.

chmod("thisfile.php", 0777);
Was This Post Helpful? 0
  • +
  • -

#8 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 04:04 PM

Tried that but still no go.

Here are the errors:

Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 60

Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 61

Warning: file_put_contents(../infofiles/twooniepot.txt) [function.file-put-contents]: failed to open stream: Permission denied in /home/shopinbo/public_html/rutherford/admin/index.php on line 62

Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 63

Warning: chmod() [function.chmod]: Operation not permitted in /home/shopinbo/public_html/rutherford/admin/index.php on line 64

here is the code:
       <?php
		if($_POST[submit]){
			$file = $_POST[type];
			$date = $_POST[date];
			$numbers = $_POST[numbers];
			$pot = $_POST[amount];
			$myfile = "../infofiles/";
			if($file == "TwooniePot"){
			//open TwooniePot.txt	
			$myfile .= "twooniepot.txt";
			}elseif($file == "TwooniePotPlus"){
			//open TwooniePotPlus.txt	
			$myfile .= "twooniepotplus.txt";
			}elseif($file == "SuperJackpot"){
			//open SuperJackpot.txt
			$myfile .= "superjackpot.txt";
			}elseif($file == "YellowStar"){
			//open YellowStar.txt	
			$myfile .= "yellowstar.txt";
			}elseif($file == "PlayersProgressive"){
			//open PlayersProgressive.txt	
			$myfile .= "players.txt";
		    }elseif($file == "Bonanza"){
			//open Bonanza.txt	
			$myfile .= "bigbuck.txt";
		    }elseif($file == "AllNothing"){
			//open AllNothing.txt	
			$myfile .= "all.txt";
	    	}else{
			//open olivestar.txt
			$myfile .= "olivestar.txt";
		    }
			$data = "$date \n";
			$data .= "$numbers \n";
			$data .= "$pot \n";
			echo $myfile;

			//$fh = fopen($myfile, 'w') or die("can't open file");
			//fwrite($fh, $data);
			//fclose($fh);	
			chmod($myfile, 0755);
			chmod("index.php", 077);
			file_put_contents($myfile,$data);
			chmod($myfile, 0644);
			chmod("index.php", 0644);
			echo "$myfile was updated.";
		}
		?>

Was This Post Helpful? 0
  • +
  • -

#9 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Problem with fopen

Posted 02 October 2010 - 04:13 PM

Do you have cpanel? If so, try to change the perms through that.
Was This Post Helpful? 0
  • +
  • -

#10 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 04:22 PM

I do have cpanel and I have not gone through and double checked through the file management part that the index file is set to 755, the admin folder it is in is set to 755, then the folder that the .txt files are in is set to 755 and all the .txt files are also set to 755. Is there a setting on the server that could be blocking this? Thank you so much for trying!
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Problem with fopen

Posted 02 October 2010 - 04:25 PM

Have you tried changing the permissions via FTP? Have you confirmed the "owner" of the files is what you expect?
Was This Post Helpful? 0
  • +
  • -

#12 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 05:07 PM

Sorry just realized there was a typo with my last post. I have double checked through cpanel that all the files and folders are set to 755, I have also checked this through ftp.

How do I check the owner name?
Was This Post Helpful? 0
  • +
  • -

#13 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 05:22 PM

okay, found out that all the usernames for all the files are the same - my user name to access my cpanel account. Is this what it is supposed to be?
Was This Post Helpful? 0
  • +
  • -

#14 kssulliv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-January 09

Re: Problem with fopen

Posted 02 October 2010 - 06:45 PM

New update: I have been working on this on my work server so I decided to try it on my personal hosting account just to check and it works perfectly. Which is great but it will have to eventually run on the work server. Any suggestions on what could be different? Is this something I can fix myself in settings or is this something I should just have my boss contact the host to have it changed/updated? Your help is greatly appreciated. Thanks!!!
Was This Post Helpful? 0
  • +
  • -

#15 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Problem with fopen

Posted 03 October 2010 - 01:46 AM

I'm guessing that your scripts are running on a *nix server. If that's the case it's almost guaranteed that your PHP processes id and FTP id are different. Meaning there's some sort of conflict. Sometimes chmod cannot be applied after the file has already been created by the PHP script, it's a result of the *nix file creation. Instead you need to take a look at a function called umask. This allows you to set the permissions at the moment the file is created. That is of course working under the assumption you have the necessary permission to use that function. If you open up with some FTP program like FileZilla for instance and take a look at the ids of the file (group, owner, etc.) you'll likely note the different numbers.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2