0 like 0 dislike
686 views
I want to edit the text files in a directory with PHP
in Education & Reference by
retagged | 686 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 6, 2013 in Education & Reference by Tisha | 549 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 27 in Education & Reference by Marc (2.8k points) | 13 views
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
asked Jan 4, 2018 in Education & Reference by Sam (1.4k points) | 367 views
0 like 0 dislike
1 answer
asked Aug 1, 2017 in Education & Reference by Krish (820 points) | 151 views
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Jul 7, 2014 in Education & Reference by Bhoomika (50 points) | 12.1k views

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,225 questions
1,370 answers
559 comments
2,099 users