Web programming >> some php-file problems
Posted by Henning on 02:19:00 01-21-2003
hey guys PLEASE help me:
I wanted to programm some php program with I can edit my files here is the code:
<html>
<head>
</head>
<body>
<form action="<? $PHP_SELF ?>" method="post">
<textarea name="data"><? readFile($file); ?></textarea>
<input type="submit">
</form>
</body>
</html>
<?
$fp = fopen("$file" , "w+");
fwrite($fp, "$data");
fclose($fp);
?>


I start it with: editfile.php?file=log.txt
It just don't work so just hgelp me
THX [addsig]
Posted by dxprog on 03:22:00 01-21-2003
It looks like you're trying to call an undefined function readFile (and I'm sure that's what it's telling you too). You need to write the read file function first. [addsig]
Posted by KaGez on 21:21:00 01-23-2003
http://www.php.net/manual/en/function.readfile.php

RTFM!
According to the informations I got out of that page, the usage of the function seems to be correct. Also, you (apparently) don't need to link it to any special libs for it. So, the best method for getting a message about what went wrong would probably be to print the contents of $php_errormsg.
Also, I don't know if it is only looking like it in the snippet you posted above, but is $file not declared? I couldn't find it in the snippet you posted there. Or is it defined in the URL?
Also, you could simply do include($file) for some things. I think if you want to output the contents of a text file, include() should work too.
[addsig]
Posted by HelloWorld on 08:16:00 07-21-2003
Hey,

I saw this post earlier on, and then I decided to write a small script so it would be easier to learn off it. Then I realised this post begun like 7 months ago, and you probably already have the soloution. But I thought, hell I whipped up a quick script, maybe it will help someone, and I'll post it anyway. So here you go.

(Note: This doesn't use readfile() anymore, as I don't like it as it goes straight to the output buffer, which means it echoed straight away.)
Code:
<?php
// Very BETA file manipulation in php.
// Edit before any commercial use, just made for an example.
// You would have to add support for CHMOD and make a login scheme to protect files.

// Writing to the file.
if ($_POST["action"] == "update" && !empty($_POST["fcontents"]) && !empty($_POST["file"])) {
if(!$fp=@fopen($_POST["file"],"wb")) { // Opens for writing, binary flag is for windows support.
echo "Failed opening ".$_POST["file"]." for writing. This may not be writable.";
}
else {
if (!@fwrite($fp,$_POST["fcontents"])) { // If doesn't write contents/
echo "Failed Writing to ".$_POST["file"];
}
$_GET["file"]=$_POST["file"];
}
}
// Reading The File
if (!empty($_GET["file"])) {
$fp=@fopen($_GET["file"],"r");
if (!$fp) {
echo "Failed to open file.";
}
else {
while(!feof($fp)) { // While not at EOF
$contents.=fgets($fp,4096); // Get contents.
}
$contents=htmlspecialchars($contents); // We need to change tag (< and >) to there html entities, so it will work.
echo "<form name=\"file\" method=\"post\" action=\"".$GLOBALS["PHP_SELF"]."\">";
echo "<input type=\"hidden\" name=\"file\" value=\"".$_GET["file"]."\">";
echo "<textarea name=\"fcontents\" cols=\"50\" rows=\"20\">";
echo $contents;
echo "</TEXTAREA><br>";
if (is_writeable($_GET["file"])) { // If file can be edited, add an update button.
echo "<input type=\"submit\" name=\"action\" value=\"update\">";
}
else { // Else tell them they cannot update it.
echo "This file is not writable.";
}
echo "</form>";
}
fclose($fp); // Close file.
}

// Directory Listing.
$dir=opendir("."); // Open current dir.
echo "<table>";
echo "<tr><td>Filename</td><td>Filesize</td></tr>";
while (false !== ($file = readdir($dir))) { // A loop to collect all files, strange looking loop is needed.
if (!is_dir($file) && is_readable($file) && !is_executable($file)) { // If the selected file is not a directory, executable and is readable.
echo "<tr><td><a href=\"".$_GLOBALS["PHP_SELF"]."?file=$file\">$file</a></td>";
echo "<td>".filesize($file)."</td></tr>";
}
}
echo "</table>";
?>


[ This Message was edited by: dxprog on 2003-07-22 05:14 ]

[ This Message was edited by: HelloWorld on 2003-07-22 08:24 ]