Page 1 of 1

spilling my beans

Posted: Fri Apr 25, 2003 10:18 pm
by wallabee
after about two or three hours of frustration, i've decided i'm just going to post my code and see if anyone can help me out. hopefully most of you can see past my amateur coding and really figure out what the root of the problem is. my aim is to get the text from $file, a text file, into a string, and eventually, into a text area.

Code: Select all

<?php
//error_reporting(E_ALL);
$PageTitle="{$_GET[DirName]} File Settings";
$HeaderTitle="{$_GET[DirName]} File Settings.";
$Current = getcwd();
require("header.php");

//This function takes text from the text file and outputs it.  The root of my problems lie in what $TheFile should be, but isn't.
function FillTextArea($TheFile, $DirectoryName){
	$ThePath = "/directories/" . "$DirectoryName/" . "$TheFile";
	$Open = fopen($TheFile, "r");
	if ($Open){
		print("Sucess");
		$Data = implode('', file("$ThePath"));
		print($Data);
		fclose($Open);
	} else {
		print("Not working");
	}
}
//Here I'm just making it easy to get it into a print() function
$FTA = FillTextArea();


//Beginning the form
print ("<form name="MasterForm" method="POST" action="HandleForm.php">");

//Here we go.  DirName was passed through a get function, it all depends on what the user chose for a directory.
$DirName = $_GET['DirName'];
$PictureArray = array();
$DescArray = array();

if ($handle = opendir("./directories/$DirName")) {
	while (false !== ($file = readdir($handle))) { 
		//I only want .txt files returned.  In retrospect, I didn't need to add the first two statements.
		if ($file != "." && $file != ".." && eregi(".+\.txt", $file)) {
		//There's two types of text files.  Example:  totalBuilding.txt and just Building.txt.  Below tests the former.
			if (eregi("total", $file)){
				//Just counting the number of text files to have a preset number when the text input loads.
				$PictureArray[] = $file;
				$TotalPictures = count($PictureArray);
				print("<FONT FACE="Tahoma" SIZE="2">{$file}'s number of pictures: </FONT><input type="text" name="text$file" value="$TotalPictures" size=1><BR>");
			} else {
				//Obviously, if it's the plain text file, I want a text area made.  My hopes are to have the original text from the text file in the text area when it loads.					
				echo "<FONT FACE="Tahoma" SIZE="2">{$file}'s number of pictures: </FONT><textarea name="textarea$file" cols="40" rows="8">$FTA</textarea><BR>";			
			}
    	}
	}
    closedir($handle); 
}


print ("</FORM>");
require("footer.php");
?>
As stated in the comments, I know that $TheFile is set wrong, and I"m hoping someone can help me out. I believe that's the ONLY thing keeping this script from working. The form is put out correctly, with the right number of boxes, so this works fine. The first function detecting the number of "totalXXX.txt" files works fine. the function putting out the text area works fine. the only thing that doesn't seem to work fine is the process of getting the text INTO the text area, which eventually traces back to the fact that fopen can't open the file because it can't seem to find it. (permissions are all already set on it).

I hope someone can help me out. Thanks.

-ben

Posted: Fri Apr 25, 2003 10:20 pm
by airo
Heh, ive fallen for these problems before.

in the if clauses instead of one = change it to ==. If it doesnt work after that, then ill look harder.

Posted: Fri Apr 25, 2003 10:24 pm
by wallabee
doesn't work.

i'm almost positive my problem is with the location i've specified the file to be in. i don't know how to refer to where it is, though.

i've tested this a couple different ways. if i take the script which implodes the text file and then prints it as a string and put it locally in one of the directories, and then specify one of the text files in that directory, it works fine and prints out all of the text. so the script itself works.

it's just the location of $file.

-ben

Posted: Fri Apr 25, 2003 10:39 pm
by airo
instead of $TheFile = "$DirName/$file"; try:
$TheFile = "$DirName" . "$file";

Posted: Fri Apr 25, 2003 11:24 pm
by wallabee
same results.

DirName is just one word, it doesn't have any slashes in it.

Posted: Sat Apr 26, 2003 5:43 am
by twigletmac
You've got a function in which you try and use previously defined variables but you haven't passed them to it, you can either pass them as arguments:

Code: Select all

function FillTextArea($DirName, $file) {
and then call the function using

Code: Select all

FillTextArea($DirName, $file);
or, if $DirName and $file are going to be constant names that you use whenever you call the function you can do

Code: Select all

function FillTextArea() {
    global $DirName, $file;
and then you can call it without having to pass arguments to it:

Code: Select all

FillTextArea();
Mac

Posted: Sat Apr 26, 2003 10:13 am
by wallabee
it's interesting, because i discovered this last night without checking the forums. alas, it does not work. the new function is:

Code: Select all

function FillTextArea ($TheFile, $DirectoryName){
    $ThePath = "/Directories" . "/$DirectoryName"."/$TheFile";
    echo $ThePath;
    $Open = fopen($ThePath, "r");
    if ($Open){
        $Data = implode('', file("$TheFile"));
        print($Data);
        fclose($Open);
    }
}
it doesn't work though. i get the echo back:
/Directories/Admissions/Admissions.txt

which is the correct echo for the directory and file relative to the main directory. if you navigate from the directory which this file is in, it should be "directories/admissions/admissions.txt", so I don't understand why it doesn't work. I get the error:

Warning: fopen(/Directories/Admissions/Admissions.txt) [function.fopen]: failed to create stream: No such file or directory in C:\Inetpub\XXwebsite\htdocs\ben\php\final\EditDirectory.php on line 22

anyone? this seems like way too big of a problem for something this simple. oh, by the way, i initialized the function using FillTextArea($file, $DirName).

edit: This function when placed locally in the folder with admissions.txt, and when $ThePath = "admissions.txt", works fine.

Posted: Sat Apr 26, 2003 10:28 am
by volka
if the error occurs in C:\Inetpub\XXwebsite\htdocs\ben\php\final\EditDirectory.php
/Directories/Admissions/Admissions.txt will point to C:\Directories\Admissions\Admissions.txt.
That's what you want?

Posted: Sat Apr 26, 2003 10:43 am
by wallabee
no, not at all.

i want c:\inetpub\xxwebsite\ben\php\final\directories\admissions\admissions.txt

so should the string then be:
"/inetpub/xxwebsite/ben/php/final/directories/$DirName/$file"?

Posted: Sat Apr 26, 2003 10:46 am
by wallabee
the echo:
Inetpub/xxwebsite/htdocs/ben/php/final/Directories/Admissions/Admissions.txt


the error:
Warning: fopen(Inetpub/xxwebsite/htdocs/ben/php/final/Directories/Admissions/Admissions.txt) [function.fopen]: failed to create stream: No such file or directory in C:\Inetpub\xxwebsite\htdocs\ben\php\final\EditDirectory.php on line 22

Posted: Sat Apr 26, 2003 10:49 am
by volka
try Directories/Admissions/Admissions.txt without the leading /

Posted: Sat Apr 26, 2003 11:48 am
by wallabee
someone just posted that solution on another forum only a little while back, before you, and it works.

thanks for everyone's help. it's interesting to see how groups of people come up with the answer at the same time, not a minute before. :)