Jump to content

Recommended Posts

Posted

Maybe I'm missing something simple (usually the case). I've searched through the forums and found similar problems, but not quite the same as mine.

 

I am trying to write to a file (datafile.xml) using PHP in myfile.php.

Here is my code:

>$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";

	if ($fp = fopen($filetoopen,"r+") !== false) {

		$newcode.='<p>Blah blah blah. Test text here.</p>';

		fseek ($fp,-16,SEEK_END); //go to the end of the file, and back up 17 bits

		if (fwrite($fp,$newcode,700)!==false) 
		   echo 'Success';
		else 
		   echo 'Error';
	
		fclose($fp);
	}

 

fopen seems to work fine. The errors I'm getting are:

Warning: fseek(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/myfile.php on line 43

 

Warning: fwrite(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/for_us/myfile.php on line 46

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/for_us/myfile.php on line 54

 

I have even tried is_readable and is_writeable using $filetoopen and they are both true. I have tried other things such as fgets and get the same error. Permissions on datafile.xml are 666, and the directory 'data' is set to 777. I'm not sure what else to try. Thanks in advance for any help you can give. I'm so frustrated with this.

~LPet

Posted

Welcome to the forums lpet :)

 

Try;

 

>$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";

	$fp = fopen($filetoopen,"r+");
	if ($fp !== false) {

		$newcode.='<p>Blah blah blah. Test text here.</p>';

		fseek ($fp,-16,SEEK_END); //go to the end of the file, and back up 17 bits

		if (fwrite($fp,$newcode,700)!==false)
		   echo 'Success';
		else
		   echo 'Error';
	
		fclose($fp);
	}

Posted
Try;

 

>$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";

	$fp = fopen($filetoopen,"r+");
	if ($fp !== false) {

 

Brilliant! Thank you so much, worked like a charm. It's simple but I don't know if I would have ever figured that one out. Learning every day. Sigh of relief.

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...