Forum Moderators: coopster
What I have is a url similar to this:
http://myplace.com/dir/file.php?action=this&object=var
The action variable calls a function I've written that's going to act on the file if the file exists. The object variable is what I'm trying to use to find the file. Basically, once I GET the object into $var, I want to use that to find any files that has $var at the beginning of their filenames, then open and read. I thought ereg() would do this, but I must be implementing it wrong or something. My code looks something like (the directory handle is declared earlier):
$var = $_GET['object'];
if ($thisdir = opendir($DIR))
{
if ($file = readdir($thisdir)!== false)
{
if (ereg("^$var" ,$file))
{
/* code here */
}
else
{
echo "<p>Error opening $file.</p>";
}
closedir($thisdir);
}
}
Sorry to bother, I've tried searching on my own but I can't find anything that covers file handling in depth, especially with finding the file. All help greatly appreciated.
[edited by: jatar_k at 6:06 pm (utc) on May 24, 2003]
[edit reason] delinked example [/edit]
if (false!== ($file = readdir($thisdir)))
Yet again I get the error message. In the error message where I use the $file var, all I get is a period. Is there something else I'm doing wrong?
The next file you read will always be ".." - referring to the parent directory.
The read after that will return an actual real file!
You need to loop ignoring "." and ".."!
Hope this helps!
function tmplEdit()
{
global $TMPL_DIR;
$tmplname = $_GET['tmplFile'];
if ($thisdir = opendir($TMPL_DIR))
{
if (false!== ($file = readdir($thisdir)))
{
while ($file!= "." && $file!= "..")
{
if (ereg("^$tmplname" ,$file))
{
$file_content = fread(fopen($file, "r"), filesize($file), 1);
fclose($file);
// work on form later, worried about grabbing file content for now
echo <<<END
<form action="$_SERVER['REQUEST_URI'] method="POST">
<textarea name="thistmpl" cols="55" rows="15">$file_content</textarea>
</form>
END;
}
else
{
echo "<p>Error opening $file: No such template available.</p>";
}
}
}
}
else
{
echo "<p>Unable to open $thisdir.</p>";
}
}
I'm still looking through tutorials and references, but so far I'm coming up empty.
I'm going to see if maybe I should add the extension somewhere in the ereg function so it knows exactly what to look for. I'll edit here and let you know how it works.
edit: no go. Is there another way besides ereg to try and match the string in $tmplname to a file in the directory containing said string in the name?
I changed the code following jatar_k's snippet, so the function looks like this
function tmplEdit()
{
global $TMPL_DIR;
$tmplname = $_GET['tmplFile'];
$thisdir = @opendir($TMPL_DIR) or die("Unable to open $TMPL_DIR");
while($file = readdir($thisdir) && $file!= "." && $file!= "..")
{
if ($file == $tmplname . ".tmpl")
{
$file_content = fread(fopen($file, "r"), filesize($file), 1);
fclose($file);
// work on form later, worried with grabbing file content for now
echo <<<END
<form action="$_SERVER['REQUEST_URI']" method="POST">
<textarea name="thistmpl" cols="55" rows="15">$file_content</textarea>
</form>
END;
die;
}
else
{
echo "<p>Error opening $file: No such template available.</p>";
}
}
closedir($thisdir);
}
I'm getting the following errors on the page itself, along with an empty textbox. (site references removed)
Warning: filesize() [function.filesize]: Stat failed for 1 (errno=2 - No such file or directory) in /mypath/www/tmpl_config/functions_tmpl.php on line 599
Warning: Wrong parameter count for fread() in /mypath/www/tmpl_config/functions_tmpl.php on line 599
Warning: fclose(): supplied argument is not a valid stream resource in /mypath/www/tmpl_config/functions_tmpl.php on line 600
So I'm guessing it's saying the file doesn't exist, which it does, so it's either not matching the $tmplname variable, or just not reading the file. So opening the directory isn't the problem, it's just not finding the file based on the GET variable I'm trying to match up.
I'm wondering if there's an easier way to do all of this. I just don't know what, and I can't find a tutorial that's been helpful.
Again, I'd like to thank you for all the help given so far. At the very least, I'm learning more.
string fread ( resource handle, int length)
so this is wrong, drop the 1
fread(fopen($file, "r"), filesize($file), 1)
I think you are doing too many things at once and getting confused. Try splitting things into individual actions and you will get better error messages and therefore a better handle on what isnt happening.
your while could be split
while($file = readdir($thisdir)){
if ($file!= "." && $file!= ".."){
and this
$fp = fopen($file, "r");
$fsize = filesize($file);
$file_content = fread($fp,$fsize);
get my drift? You need to isolate the errors and deal with them one at a time, if everything is banged into one line it becomes difficult. Assign the value of a function to a var and then pass that var to the other function.
If you want to test a specific spot "if" it and then echo and die(if needed).
if ($file == $tmplname . ".tmpl")
I tried using that with an include($file), and for some reason the warning came back: Failed opening '1' for inclusion
It seems everything falls apart there, so to speak. In a nutshell, how do I go about using the string passed through the url to find a file with the same beginning name, then open and read it?
function tmplEdit()
{
global $TMPL_DIR;
$tmplname = $_GET['tmplFile'];
$thisdir = @opendir($TMPL_DIR) or die("Unable to open $TMPL_DIR");
while ($file = readdir($thisdir))
{
if ($file!= "." && $file!= "..")
{
if ($file == $tmplname . ".tmpl")
{
$file = $TMPL_DIR . $tmplname . ".tmpl";
$fp = fopen($file, "r");
$fsize = filesize($file);
$file_content = fread($fp, $fsize);
fclose($fp);
// work on form later, worried with grabbing file content for now
echo <<<END
<form action="$_SERVER['REQUEST_URI']" method="POST">
<textarea name="thistmpl" cols="55" rows="15">$file_content</textarea>
</form>
END;
}
else
{
echo "<p>Error opening file: $tmplname.tmpl. ";
echo "No such file found in directory.</p>";
}
}
}
closedir($thisdir);
}
Tried with the actual file, the contents come up in the textbox just fine. I entered a bogus file variable and the error message printed out. So everything seems to work now. Thanks again for all the help, I wouldn't have been able to figure this out otherwise. Now I can move on and start learning how to edit and save the file.
[edited by: ln_tora at 11:15 pm (utc) on May 24, 2003]
$tmplname = $_GET['tmplFile'];
$template = $TMPL_DIR . tmplname . ".tmpl";
if (file_exists($template)) {
echo "Found match. Proceed.";
}
else {
echo "no match found";
}