APD1993 Posted February 29, 2012 Share Posted February 29, 2012 I am working on a task where the user of the webpage can amend to a text file, but when I try it on Windows, I get messages such as Warning: fopen(C: mpp\htdocs cm\denman2.txt) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\amendfile.php on line 8 Warning: fputs() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\amendfile.php on line 10 Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\amendfile.php on line 11 And nothing is appended. I don't know what the problem is, considering the only aspect I really changed was the file path since I was using Ubuntu to try to accomplish this. Here is the coding of my Amend File Script: <html> <head><title>Amend File</title></head> <?php if($_POST['append'] !=null) { $filename="C:\xampp\htdocs\rcm\denman2.txt"; $file=fopen($filename, "a"); $msg="Updated Information: " .$_POST['append']. "<br>"; fputs ($file, $msg); fclose($file); } ?> <body> <h1>Do you want to append to a document?</h1> Enter Updated Information: <form action="amendfile.php" method="post"> <input type="text" size="40" name="append"><br><br> <input type="submit" value="Add name to report"> </form> <form action="viewfile.php" method="post"> <input type="submit" value="View Web Blog"> </form> <a href="viewfile.php">View Web Blog</a></body></html> Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 29, 2012 Share Posted February 29, 2012 When used inside of a double-quoted string, the \ starts an escape sequence of characters. Either use single-quotes around the string or since php under Windows converts / in file paths to the Windows \, you can simply use - $filename="C:/xampp/htdocs/rcm/denman2.txt"; Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322342 Share on other sites More sharing options...
APD1993 Posted February 29, 2012 Author Share Posted February 29, 2012 When used inside of a double-quoted string, the \ starts an escape sequence of characters. Either use single-quotes around the string or since php under Windows converts / in file paths to the Windows \, you can simply use - $filename="C:/xampp/htdocs/rcm/denman2.txt"; Thanks, that seems to have fixed the problem since I can now append to an existing text file! I still have the Notice: Undefined index: append in C:\xampp\htdocs\amendfile.php on line 5, but I guess that Windows is just being Windows, since I specified later on that "append" is the name of a form control Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322346 Share on other sites More sharing options...
PFMaBiSmAd Posted February 29, 2012 Share Posted February 29, 2012 The undefined index error was always present in your code, but was being hidden by the error_reporting/display_errors settings. You should be using isset to test for the existence of variables that might not exist when your code runs. Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322352 Share on other sites More sharing options...
APD1993 Posted February 29, 2012 Author Share Posted February 29, 2012 Another issue has cropped up with the following code that is used to display the contents of the RCM folder: <html><head> <title>RCM File List</title></head> <body> <?php $dirname="C:/xampp/htdocs/rcm/"; $dir=opendir($dirname); while (false != ($file=readdir($dir))) { if (($file !=".") && ($file !="..") { $file_list .="<li>$file</li>"; } } closedir($dir); ?> <p>Files in RCM Folder</p> <ul> <?php echo ($file_list); echo date("d/m/y : H:i:s", time());?></ul> </body> </html> The issue here is that I am being told that: Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\viewfile.php on line 8 Even though the "{" is necessary in order to start off the loop :/ Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322354 Share on other sites More sharing options...
PFMaBiSmAd Posted February 29, 2012 Share Posted February 29, 2012 The { is unexpected because you are missing a ) immediately before it. Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322358 Share on other sites More sharing options...
APD1993 Posted February 29, 2012 Author Share Posted February 29, 2012 The { is unexpected because you are missing a ) immediately before it. Thank you so much! Sorry for being a pain and asking what may seem "trivial" or "stupid" questions Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322359 Share on other sites More sharing options...
APD1993 Posted February 29, 2012 Author Share Posted February 29, 2012 Sorry again, but does anyone know how to create a "file filter" of sorts in PHP? I want to display a list of files in a certain folder, but I only want the files that just have the ".txt" file extension. This is the code so far: <html><head> <title>RCM File List</title></head> <body> <?php $dirname="C:/xampp/htdocs/rcm/"; $dir=opendir($dirname); while (false != ($file=readdir($dir))) { if (($file !=".") && ($file !="..")) { $file_list .="<li>$file</li>"; } } closedir($dir); ?> <p>Files in RCM Folder</p> <ul> <?php echo ($file_list); echo date("d/m/y : H:i:s", time());?></ul> </body> </html> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322386 Share on other sites More sharing options...
PFMaBiSmAd Posted February 29, 2012 Share Posted February 29, 2012 glob Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322389 Share on other sites More sharing options...
APD1993 Posted March 1, 2012 Author Share Posted March 1, 2012 Would anyone be able to help me being able to attach a hyperlink to each item in the Directory List? When I try something like this: <html><head> <title>RCM File List</title></head> <body> <h1>Files in RCM Directory</h1> <?php $dir="C:/xampp/htdocs/rcm/*txt"; foreach(glob($dir) as $file) { echo "<li><a href="readfile2.php">Filename: $file</a> " . "<br/>"; } ?> </body> </html> I get this error message: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\viewfile.php on line 12 Any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/257985-amending-to-a-file-on-a-windows-machine-with-xampp/#findComment-1322644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.