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

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

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

Post 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;
Last edited by howey on Thu Sep 11, 2008 5:15 pm, edited 1 time in total.
verwer
New Member
New Member
Posts: 7
Joined: Tue Aug 26, 2008 4:54 pm

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

Post by verwer »

You mean something like this?
http://www.hotscripts.com/Detailed/40848.html

grab it and change it to your needs..
viebig

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

Post 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"}
Last edited by viebig on Wed Sep 03, 2008 3:57 pm, edited 1 time in total.
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

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

Post 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.
alby

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

Post 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
viebig

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

Post by viebig »

there is a typo error in

Code: Select all


$file = $params['file];

should be

Code: Select all

$file = $params['file'];
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

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

Post 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.
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

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

Post 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
viebig

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

Post 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.
alby

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

Post 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
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

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

Post 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?
alby

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

Post 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
viebig

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

Post 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";
}
}
Post Reply

Return to “Developers Discussion”