Jump to content

Text Files?


Man In Tan

Recommended Posts

I can't always reach my server, so I threw together a file manager in PHP/XHTML. However, it can only distinguish between files and directories, so all files are read, htmlspecialchars()'d, and displayed in a <textarea/>. But some of my files aren't text, so when they are displayed, it breaks my XML, and prevents me from managing the file, including deleting files. Is there a way to distinguish text files from non-text files before deciding how to handle them?

Link to comment
Share on other sites

if you have access to the fileinfo functions, you could check the mime type, eg.

$finfo = finfo_open(FILEINFO_MIME_TYPE);$mime = finfo_file($finfo, 'example path.txt');finfo_close($finfo); if ($mime == 'text/plain') {//}

or if you're sure the file extensions will always refer to the actual content of the files, you could maybe have an array of binary known extensions check against the extension. or if chr(0) is causing the problem, you could read the file contents in a loop (eg. 4096 bytes per iteration) and check if chr(0) is found in the data with strpos().

Link to comment
Share on other sites

Thanks a lot! That seems to be doing the trick. I just need to make a list of text content-types. Most of them can be checked by explode()ing $mime and checking for "text" (or strpos()ing for "text/"), but things like XHTML will need to added to a list. I do have one more question, though.Sometimes, it tells me that permission was denied, which make sense, but other times, it gives me this message.

Warning: finfo_file(/Volumes/Macintosh/Applications) [function.finfo-file]: failed to open stream: No such file or directory in /Volumes/WebServer/0gb.us/admin/filemanager.php on line 20
I know the files and directories exist, and are spelled correctly, because I used readdir() to find them. Any idea why PHP thinks the file/directory exists for readdir(), but not for finfo_file()? I checked the manual, but found nothing.
<?phpif($handle = opendir('/Volumes/WebServer/'.$_GET['file'])):while(false !== ($file = readdir($handle))):  echo '<a href="/admin/filemanager.php?file=',   urlencode($_GET['file']),'%2F',   urlencode($file),'">',$file,"</a><br />\n";endwhile;closedir($handle);endif;

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...