php - Selecting file to be edited -
i have application used edit .txt files. application made of 3 parts
displays contents of folder files edited(each file link when clicked opens on edit mode).
writing in file.
saving file.
part 2 , 3 have completed using fopen , fwrite functions wasn't hard. part need part 1 open file inputing location , file name in php file have display function , save function:
$relpath = 'file_to_edit.txt'; $filehandle = fopen($relpath, 'r') or die("failed open file $relpath ! ");
but want file open in edit mode when clicked instead of typing in files name every time.
$directory = 'folder_name'; if ($handle = opendir($directory. '/')){ echo 'lookong inside \''.$directory.'\'<br><br>'; while ($file = readdir($handle)) { if($file !='.' && $file!='..'){ echo '<a href="'.$directory.'/'.$file.'">'.$file.'<a><br>'; } } }
this code ti use display list of files in specified folder. can give me pointers how can achieve ? appreciated.
- to content of file use file_get_contents();
- to put content of file use file_put_contents(); file_append flag editing.
- to recieve list of files in directory can use directoryiterator
example:
foreach (new directoryiterator('path/') $fileinfo) { if($fileinfo->isdot()) continue; echo $fileinfo->getfilename() . "<br>\n"; }
if don't want put filenames can put read files once put in db assign ids them , use links id param. other solution store files in session array , assign keys them. when want file need provide key instead of whole filename , path.
example $_session
$file_arr = array(); foreach (new directoryiterator('path/') $fileinfo) { if($fileinfo->isdot()) continue; $file_arr[] = array("path" => $fileinfo->getpathname(), 'name' => $fileinfo->getfilename()); } $_session['files'] = $file_arr;
then in view can use
foreach($_session['files'] $k=>$file) { echo "<a href='edit.php?f=".$k."'>'.$file['name'].'</a>"; }
and edit.php
$file = (int)$_get['f']; if(array_key_exits($file, $_session['files']) { $fileinfo = $_session[$file']; //in file info have $fileinfo['path'] $fileinfo['name'] }
Comments
Post a Comment