Jump to content

Amending to a file on a Windows machine with XAMPP


APD1993

Recommended Posts

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! :D

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :/

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.