Jump to content

File Writing


Andrew K.

Recommended Posts

Okay so I've never really used PHP before (I'm more familiar with ColdFusion), but I need to use PHP(My ColdFusion host doesn't support file writing).So I found this code and managed to edit it but something is going wrong. (php.net, I am allowed to use examples from there as codes right?)I am trying to get the file contents and append the query string to the file but it just won't work.Perhaps someone can help me, it doesn't matter how much of this script is changed as long as it does what I want. Also, instead of echoing that stuff, I would like the script just to redirect to the referer.:)

<?php$filename = 'data.txt';$begin= file_get_contents("http://h1.ripway.com/AndrewK/innovate/data.txt",FALSE);$end = "Add this to the file\n //--\\";if (is_writable($filename)) {   if (!$handle = fopen($filename, 'a')) {		 echo "Cannot open file ($filename)";		 exit;   }   if (fwrite($handle, $begin . $QUERY_STRING) === FALSE) {	   echo "Cannot write to file ($filename)";	   exit;   }     echo "Success, wrote ($somecontent) to file ($filename)";     fclose($handle);} else {   echo "The file $filename is not writable";}?>

Errors occured: Warning: file_get_contents(): URL file-access is disabled in the server configuration in \\192.168.0.16\webfiles\files\2006-1\616654\innovate\write.php on line 3Warning: file_get_contents(http://h1.ripway.com/AndrewK/innovate/data.txt): failed to open stream: no suitable wrapper could be found in \\192.168.0.16\webfiles\files\2006-1\616654\innovate\write.php on line 3Success, wrote () to file (data.txt)

Link to comment
Share on other sites

It looks like your host disabled the option to allow file_get_contents to work with http. If they did that, I'm almost positive that the same restriction would apply to something like fopen. You may need to use FTP to get the file, so hopefully you have FTP access to the h1.ripway.com server.That just made me think about something. Are the PHP file (the one you posted) and the data.txt on the same server? If they are both on the same server, then all you need to do to fix this is change the filename. So instead of telling file_get_contents to open this file:http://h1.ripway.com/AndrewK/innovate/data.txtYou would need to give it the filesystem path, so it would maybe be something like this (I'm guessing from the info in the error message):\\192.168.0.16\webfiles\files\2006-1\616654\innovate\data.txtOr, if the PHP script and data.txt are in the same folder, then you can just do this:file_get_contents("data.txt");When you use http with file_get_contents (or fopen and the like), it actually goes out to the server and makes a connection, even if it is the same server. It is more efficient to just access it through the local filesystem. But your host has disabled the option to use http with the file functions, like I mentioned.But, if the files are on two different servers, then you will probably need to use the PHP FTP functions to open an FTP connection, browse to the folder that contains data.txt, download it to the local server, and then use file_get_contents on the local copy.

Link to comment
Share on other sites

Or, if the PHP script and data.txt are in the same folder, then you can just do this:file_get_contents("data.txt");
Thank you it worked perfectly.Now all I need to know is how to redirect it to the page referrer.:)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...