Claim your Biolink Click Here
0 like 0 dislike
815 views
I want to edit the text files in a directory with PHP
in Education & Reference by
retagged | 815 views

2 Answers

0 like 0 dislike
Best answer
Show all files in a folder "filesinfolder.php"
 
<?php
function show_files($dir)
     if(is_dir($dir)) 
    { 
        if($handle = opendir($dir)) 
        { 
            while(($file = readdir($handle)) !== false) 
            { 
                if($file != "." && $file != "..")
                { 
                    echo '<a target="_blank" href="editfiles.php?filename='.$file.'">'.$file.'</a>'."<br>"; 
                } 
            } 
            closedir($handle); 
        } 
    } 
}
show_files('C:\xampp1\htdocs\test'); //path where the directory is
?>
 
--------------------------------------------------
secondly Edit the file "editfiles.php"
------------------------------------------------
<?php
//$url = 'http://127.0.0.1/test/file.php';
$file = $_GET['filename'];
 
// check if form has been submitted
if (isset($_POST['text']))
{
    // save the text contents
    file_put_contents($file, $_POST['text']);
}
 
// read the textfile
$text = file_get_contents($file);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text" rows="15" cols="20"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>

 

by
0 0
I'm now looking for a way to list ONLY file that have .txt (or .TXT) at the end, how do I do that?
Code is appreciate, thanks!
0 like 0 dislike

In filesinfolder.php

Modify the line by this

if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'txt')

by

Related questions

2 like 0 dislike
1 answer
asked Aug 7, 2013 in Education & Reference by Tisha | 731 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
asked Jun 17, 2021 in Education & Reference by Simmi (830 points) | 285 views
0 like 0 dislike
1 answer
asked May 27, 2020 in Education & Reference by Marc (4.6k points) | 224 views
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
asked Jan 4, 2018 in Education & Reference by Sam (1.6k points) | 1.1k views
0 like 0 dislike
1 answer
asked Aug 1, 2017 in Education & Reference by Krish (1.1k points) | 492 views
0 like 0 dislike
2 answers

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,308 questions
1,469 answers
568 comments
4,809 users