All pastes #805474 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-12-04 11:47 UTC
rendered paste body
<?php

// 	filetype.php
//	(c) 2007 enteam.ru ;-)

/*
	script gets file MIME type and shows it's contents if that is plain or html text,
	shows an image if that is any image file, and shows hex data if that is some other type.
	using Fileinfo extension (php_fileinfo.dll or so on).
*/

$title = "myFileGodFather v0.1a";

$space = "  ";

echo "<html>\n";
echo "<head>\n$space<title>$title</title>\n</head>";
echo "<body>\n";


echo "$space<h1>$title</h1>\n";

if ($_GET['file'] && file_exists(realpath($_GET['file']))) {
	$file = $_GET['file'];
	$filehandleinfo = finfo_open(FILEINFO_MIME);
	$fileinfo = chop(finfo_file($filehandleinfo, realpath($file)));
	echo "$space<h2>" . $file . ", " . $fileinfo . "$space</h2>\n";
	if (preg_match("/^text\/.+/i", $fileinfo)) {
		// печатаем текст...
		$filecontents = file($file);
		if (preg_match("/.+html.*/i", $fileinfo)) {
			// html...
			foreach ($filecontents as $line) {
				echo htmlspecialchars($line) . "\n";
			}
		} else {
			// plain text...
			echo "$space<pre>\n";
			foreach ($filecontents as $line) {
				echo $line . "\n";
			}
			echo "$space</pre>\n";
		}
	} elseif (preg_match("/^image\/.*/i", $fileinfo)) {
		// показываем картинку...
		echo "$space<img src=\"" . realpath($file) . "\" alt=\"$file\" />\n";
	} else {
		// выводим hex...
		echo "\n";
	}
}

echo "$space<form id=\"file_main\" name=\"file_get\" method=\"GET\">\n";
$space = "    ";
echo "$space<input id=\"file_name\" name=\"file\" type=\"text\" width=\"50\" />\n";
echo "$space<input id=\"file_ok\" name=\"Получить\" type=\"submit\" width=\"20\" />\n";
$space = "  ";
echo "$space</form>\n";

echo "</body>\n";
echo "</html>\n";
?>