Jump to content

How to open & edit file in list content of directory


mrbhb

Recommended Posts

Hello Everyone, I’m having a page php have function list content of directory in computer such as c:/folder after that I can open, write and save any file I want in list file. Now I have complete to list content of directory, now I want to click any file in list file of derectory to open in a textarea under list content of drictory to view and edit file, then I have a button submit to save it. I don’t know how to do it so please fix my code I give under introduce me some example demo code or document to reference. Please help me. Thank very much.


Here is my code:


https://www.mediafire.com/?19123qkmgmf7toj


Example picture about page equivalent and similar:


In this picture it have list file.


f1_zpsd8327c90.jpg


Now we can click to open 1 file such as “extensions.conf” to read, write and save it by button submit (update)


f2_zpsf48caae7.jpg


here is my code, I need some help. plz

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
table
{
width: 100%;
padding: 5px 5px 0px 5px;
text-decoration:none;
}
a
{
text-decoration:none;
}
h3
{
padding: 5px 0px 5px 0px;
color:#000;
}
tr
{
height: 20px;
}
</style>
</head>


<body>
<!--code show file cua thu muc-->
<?php
echo "<h3>LIST FILE OF FOLDER</h3>";
// directory
$dir = "";


        // Opens directory
        $myDirectory=opendir(".");
        
        // Gets each entry
        while($entryName=readdir($myDirectory))
{
$dirArray[]=$entryName;
        }


// Finds extensions of files
        function findexts ($filename) 
{
$filename=strtolower($filename);//chuyen sang chu thuong
           $exts=split("[/\\.]", $filename);//cat chuoi
           $n=count($exts)-1;
           $exts=$exts[$n];
           return $exts;
}


// Closes directory
        closedir($myDirectory);
        
        // Counts elements in array
        $indexCount=count($dirArray);
        
        // Sorts files
        sort($dirArray);
        
// Print list content directory
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Name</TH><th>Type</th><th>Size</th><th>Date Modified</th></TR>\n");
// Loops through the array of files
        for($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != ".")
{ 
//File name
$name=$dirArray[$index];
$namehref=$dirArray[$index];


// Gets File Extensions 
           $extn=findexts($dirArray[$index]);


// File type
switch ($extn)
{
case "png": $extn="PNG Image"; break;
case "jpg": $extn="JPEG Image"; break;
case "bmp": $extn="BITMAP Image"; break;
case "gif": $extn="GIF Image"; break;
case "ico": $extn="Windows Icon"; break;
case "txt": $extn="Text File"; break;
case "log": $extn="Log File"; break;
case "htm": $extn="HTML File"; break;
case "php": $extn="PHP Script"; break;
case "js": $extn="Javascript"; break;
case "css": $extn="Stylesheet"; break;
case "pdf": $extn="PDF Document"; break;
case "zip": $extn="ZIP Archive"; break;
case "docx": $extn="Microsoft Word Document"; break;
case "mp3": $extn="MP# Format Sound"; break;
default: $extn=strtoupper($extn)." File"; break;
           }
          
           // Gets file size 
           $size=number_format(filesize($dirArray[$index]))."&nbsp"."KB";
          
           // Gets Date Modified Data
           $modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
           $timekey=date("YmdHis", filemtime($dirArray[$index]));


print("
           <tr>
             <td><a href='./$namehref'>$name</a></td>
             <td><a href='./$namehref'>$extn</a></td>
              <td><a href='./$namehref'>$size</a></td>
             <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td>
           </tr>");
/*print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(findexts($dirArray[$index]));
//print(filetype($dirArray[$index]));
print("</td>");
print("<td>");
print(filesize($dirArray[$index]));
print("</td>");
print("<td>");
print($modtime);
print("</td>"); 
print("</TR>\n");*/
}
}
print("</TABLE>\n");
?>
<!--code doc sua va luu 1 file bat ky-->
<br />
<h3>OPEN, EDIT AND SAVE ANY FILE IN LIST FILE OF FOLDER</h3>
<br />
        <form name="read_form" method="post" action="">
        <textarea name="read_file" cols="110" rows="35" wrap="off">
        </textarea>
        <br />
        <input type="submit" name="update" value="UPDATE" />
        </form>
</body>
</html>
I still don't know how to when I click to file name in list content of directory, it content have show in textarea, what event click to to it ? I have read a example simple below but it open and save file have direct directory.


<?php
 
$filename = isset($_GET['file']) ? $_GET['file'] : '';
$directory = ("c:/folder/file.php"); // Restrict to this directory...
 
$fn = $directory . $filename;
 
if (isset($_POST['content']))
{
    $content = stripslashes($_POST['content']);
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!");
    fputs($fp,$content);
    fclose($fp) or die ("Error closing file!");
}
 
?>
 
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
    <textarea rows="25" cols="100" name="content"><?php readfile($fn); ?></textarea>
    <hr />
    <input type="submit" value="Save">
</form>


Archived

This topic is now archived and is closed to further replies.

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