Page 1 of 1

filelist TAG – get a text file to display its content [solved - I think]

Posted: Tue Sep 02, 2008 8:39 pm
by howey
Hi

Rather than generate a random list of files I was trying to get a random text file from the specified directory, and then display it's content. This would operate in a similar way to the random quote tag, but using separate text files for each item, also it would enable text files to be uploaded to a directory in the uploads directory through the file manager. I don't want to give global access through the file manager for security reasons etc.

I tried using the filelist TAG but couldn't get the contents of the file to print. Alternatively I tried setting parameters in the random_quote TAG, but kept getting error messages .

Code: Select all

	
#Check if user has specified a file, otherwise use default
if (isset($params['file']))
{$quotefile = $params['file'];} 
else {$quotefile = "quotes.txt";}
if I put "uploads/directory/quotes.txt" or variations on this. Would I need to set the file path as a variable to be included in the parameter. Athough this would involve putting all the text in one file.

With the filelist TAG I tried assigning the "file" as a variable and using the code from the random quote TAG to display the contents

when I assigned the fillist with the variable quote

Code: Select all

$fp = fopen($quote);
$contents = fread($fp, filesize($quote));
fclose($fp);
return $contents;

Re: filelist TAG – How can I get a text file to display its content

Posted: Tue Sep 02, 2008 9:56 pm
by verwer
You mean something like this?
http://www.hotscripts.com/Detailed/40848.html

grab it and change it to your needs..

Re: filelist TAG – How can I get a text file to display its content

Posted: Wed Sep 03, 2008 3:50 pm
by viebig
well, that´s easy, I´ll give you a simple example:

create a udf and name it quotefile

case: get one specific file content

Code: Select all

$dir = $params['dir'];
$file = $params['file];
$result = file_get_contents($dir.$file);
return $result;

use it like:

Code: Select all

{quotefile dir="/var/home/" file="file.txt"}
case: get a random file content

Code: Select all

$dir = $params['dir'];
$file = $params['file];
if($file == "random")
{
$dp=opendir($dir);

$i=0;
while($files=readdir($dp))
{
     if ($files != "." && $files !="..")
         {
        $filesarray[$i]=$files;
        $i++;
        }
}
closedir($dp);

$rand=rand(0,count($filesarray)-1);
$result = file_get_contents($dir.$filesarray[$rand]);
return $result;
}
else
{
$result = file_get_contents($dir.$file);
return $result;
}

use it like:

Code: Select all

{quotefile dir="/var/home/" file="random"}
{quotefile dir="/var/home/" file="file.txt"}

Re: filelist TAG – How can I get a text file to display its content

Posted: Fri Sep 05, 2008 3:00 pm
by howey
Hi viebig

Thanks for the reply (took me some time).

I tried using the code you posted, creating a UDT in the backend. But unfortunately it would't let me submit the code. Returning an error on the last line of code. I tried working around it but still got errors.

# Parse error: syntax error, unexpected $end in /var/www/vhosts/etc : eval()'d code on line 26

I don't think I'm quite up to speed on the programming side yet.

Re: filelist TAG – How can I get a text file to display its content

Posted: Fri Sep 05, 2008 3:21 pm
by alby
howey wrote: # Parse error: syntax error, unexpected $end in /var/www/vhosts/etc : eval()'d code on line 26
Add un extra empty row to end of code

Alby

Re: filelist TAG – How can I get a text file to display its content

Posted: Fri Sep 05, 2008 5:04 pm
by viebig
there is a typo error in

Code: Select all


$file = $params['file];

should be

Code: Select all

$file = $params['file'];

Re: filelist TAG – How can I get a text file to display its content

Posted: Sat Sep 06, 2008 1:15 pm
by howey
Cheers Viebig, I had read it through a couple of times but was looking towards the end.

I'll let you know how I get on.

Re: filelist TAG – How can I get a text file to display its content

Posted: Thu Sep 11, 2008 5:14 pm
by howey
Managed to more or less solve it with an amalgamation of Viebigs code and the random image TAG. Also managed to add a content block to pass a variable so that the "editor" can choose the directory that the files are in.

It's great to have a few signpost along the way – I can then try and solve the issue myself. I get more satisfaction out of working through the problem rather than being spoon fed. You never know one day I might be able to say that I can code  ;D

Re: filelist TAG – get a text file to display its content [solved - I think]

Posted: Thu Sep 11, 2008 9:25 pm
by viebig
that was the idea! now you can enjoy your work, and be proud of yourself!  :)

Next time you can be the one to help users on a issue.

Re: filelist TAG – How can I get a text file to display its content

Posted: Thu Sep 11, 2008 9:39 pm
by alby
howey wrote: Managed to more or less solve it with an amalgamation of Viebigs code and the random image TAG. Also managed to add a content block to pass a variable so that the "editor" can choose the directory that the files are in.
Why not publish your code in "Share your tags here" in wiki?

Alby

Re: filelist TAG – get a text file to display its content [solved - I think]

Posted: Mon Sep 22, 2008 1:14 pm
by howey
I'll post the TAG as soon as I have corrected one slight issue.

If the content of the directory is empty the TAG returns either a blank or in some cases "." or var.

It must be addressing the directory?

Re: filelist TAG – get a text file to display its content [solved - I think]

Posted: Mon Sep 22, 2008 1:34 pm
by alby
howey wrote: I'll post the TAG as soon as I have corrected one slight issue.

If the content of the directory is empty the TAG returns either a blank or in some cases "." or var.

It must be addressing the directory?
viebig code is an example.
You must add checks if exist $filesarray and/or $dir.$file

Alby

Re: filelist TAG – get a text file to display its content [solved - I think]

Posted: Wed Sep 24, 2008 3:35 pm
by viebig
something like that

Code: Select all

$dir = $params['dir'];
$file = $params['file];
if($file == "random")
{
if (file_exists($dir))
{
$dp=opendir($dir);

$i=0;
while($files=readdir($dp))
{
     if ($files != "." && $files !="..")
         {
        $filesarray[$i]=$files;
        $i++;
        }
}
closedir($dp);
}
if(count($filesarray) > 0)
{
$rand=rand(0,count($filesarray)-1);
$result = file_get_contents($dir.$filesarray[$rand]);
return $result;
}
else
{
return "No files found";
}
}
else
{
return "Directory not found";
}
else
{
if (file_exists($dir.$file))
{
$result = file_get_contents($dir.$file);
return $result;
}
else
{
return "File not found";
}
}