catalyst2 community forums  

Go Back   catalyst2 community forums > Support > Scripting Support

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 19-03-2006, 01:02 PM   #1 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
Reading and writing to external .txt files

To write to a .txt file in CGI, I am using the following piece of code:

open (LOG, ">>../../../logs/logfile.txt") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
print LOG "$somecomments\n";
close (LOG) || Error ('close', 'file');


My question is, what is the path to access the 'logs' directory on my site? i.e. what do I put in the ../../../ ??

Thanks in advance
Stephen
CAT4361 is offline   Reply With Quote
Old 19-03-2006, 02:26 PM   #2 (permalink)
Bring me your problems :p
 
paulredpath's Avatar
 
Join Date: Jan 2003
Location: /dev/ahhhhhhhhh
Posts: 3,537
Hey

Will be d:\domains\yourdomain\logs , however I am not 100% sure if perl requires double backslashes...
paulredpath is offline   Reply With Quote
Old 19-03-2006, 02:57 PM   #3 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
I've tried it with double blackslashes, single backslashes, domain with and without extension .. still coming up permission denied or invalid directory?? any other suggestions??

open (LOG, ">>d:\\domains\\weemedesigns.co.uk\\logs\\file.txt ") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
print LOG "WOHOO !!\n";
close (LOG) || Error ('close', 'file');
CAT4361 is offline   Reply With Quote
Old 20-03-2006, 12:46 AM   #4 (permalink)
Administrator
 
Jacob's Avatar
 
Join Date: May 2003
Posts: 1,299
Can you get it to print the directory it is attempting to look at?
Jacob is offline   Reply With Quote
Old 20-03-2006, 02:01 AM   #5 (permalink)
Bring me your problems :p
 
paulredpath's Avatar
 
Join Date: Jan 2003
Location: /dev/ahhhhhhhhh
Posts: 3,537
Steve,

Try // slashes.
paulredpath is offline   Reply With Quote
Old 20-03-2006, 08:08 PM   #6 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
Paul, tried:

//
/
\\
\

with and without extensions ... still nothing??

Jacob,

Tried this piece of code to print out the directory:


opendir (LABEL, ".") || Error ('open', 'directory');
my @logfiles = readdir (LABEL);
close (LABEL);

if (@logfiles)
{
print "following files are available...";
foreach(sort @logfiles)
{
print"<LI>$_" if /^logfile/;
}
}


To try and print the current directory and changed to try parent directories i.e. " ../" and "../../" etc... but nothing

Maybe I should have gone for a UNIX server ...??
CAT4361 is offline   Reply With Quote
Old 21-03-2006, 07:18 PM   #7 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
I have managed to get a print out of the directory by 'globbing' it. The code I used for it is:


#!D:\Perl\bin\Perl.exe wT
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);


my @files_found;

@files_found = <D:\\domains\\weemedesigns.co.uk\\logs\\*>;

print "Content-type: text/html \n\n";
print "@files_found";

open (LOGFILE, ">D:\\domains\\weemedesigns.co.uk\\wwwroot\\cg i-bin\\file.txt") || die ('open', 'file');

if(-e LOGFILE)
{
print "File exists.";
}
if(-w LOGFILE)
{
print "File writable.";
}
if(-x LOGFILE)
{
print "File executable.";
}
if(-o LOGFILE)
{
print "File owned by user.";
}
if(-z LOGFILE)
{
print "File exists and has zero.";
}
if(-f LOGFILE)
{
print "Entry is a plain file.";
}
if(-d LOGFILE)
{
print "Entry is a directory.";
}
if(-T LOGFILE)
{
print "File is TEXT.";
}
if(-B LOGFILE)
{
print "File is BINARY.";
}



I got the following output:


D:\domains\weemedesigns.co.uk\logs\file.txt D:\domains\weemedesigns.co.uk\logs\W3SVC2902

Software error:
openfile at d:\domains\weemedesigns.co.uk\wwwroot\cgi-bin\add_to_basket.pl line 19.
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.



It seems to be having a problem with opening the file. Any suggestions why this is? What version of perl is installed? I know that versions earlier than 5.6 have problems with the 'warning' system i.e. -w in the shebang line - I have posted about this problem previously.
CAT4361 is offline   Reply With Quote
Old 22-03-2006, 01:24 AM   #8 (permalink)
Bring me your problems :p
 
paulredpath's Avatar
 
Join Date: Jan 2003
Location: /dev/ahhhhhhhhh
Posts: 3,537
We run at least 5.6.1, any other perl boffins around who have an idea?
paulredpath is offline   Reply With Quote
Old 23-03-2006, 03:24 PM   #9 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
Going to try going down the PHP root instead. But would still be interesting to find out whats wrong with the CGI ..

Thanks for all your help so far guys!
CAT4361 is offline   Reply With Quote
Old 23-03-2006, 05:17 PM   #10 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
What A Headache!!

Using PHP now ... same problem!! I've used the following script:


<?
// Define the full path to your folder from root
$path = "D:\\domains\\weemedesigns.co.uk\\wwwroot\\";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files
while ($file = readdir($dir_handle))
{
if($file == "." || $file == ".." || $file == "index.php" )
continue;
echo "<a href=\"$file\">$file</a><br />";
}

// Close
closedir($dir_handle);



// Write to data file

// Define file path
$data_path = "D:\\domains\\weemedesigns.co.uk\\wwwroot\\data.tx t";

// Open file
$open = fopen ($data_path, "w") || error_reporting(E_ALL);

// Write to file
fwrite ($open, "HELLO??") || error_reporting(E_ALL);

// Close file
fclose ($open) || error_reporting(E_ALL);
?>



The first part of the script prints out the wwwroot of the directory, the second part writes to the .txt file.

This time I can get a print out of the wwwroot directory (WOHOO!!) but then I get the following errors when trying to write to the .txt file:


Warning: fwrite(): supplied argument is not a valid stream resource in d:\domains\weemedesigns.co.uk\wwwroot\test_php.php on line 26

Warning: fclose(): supplied argument is not a valid stream resource in d:\domains\weemedesigns.co.uk\wwwroot\test_php.php on line 27



Is it maybe something to do with permissions set on the file? And if so how do you change the file permissions ... I tried through FTP (right clicking on the file) but it says permissions cannot be changed.

Or maybe something to do with firewalls? These are the only things I can come up with .. I think from trying to access files in Perl and PHP it's proven it's not my code and it's not the wrong path to the file. I've no idea!!

Surely someone else must have tried to write to an external .txt file - TELL ME HOW !!
CAT4361 is offline   Reply With Quote
Old 23-03-2006, 05:29 PM   #11 (permalink)
Administrator
 
Jacob's Avatar
 
Join Date: May 2003
Posts: 1,299
I have updated your permissions, please try that now.

Regards,

Jacob
Jacob is offline   Reply With Quote
Old 23-03-2006, 05:32 PM   #12 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
One of the errors has disappeared just left with:

Warning: fclose(): supplied argument is not a valid stream resource in d:\domains\weemedesigns.co.uk\wwwroot\test_php.php on line 27

But nothing is written to the .txt file
CAT4361 is offline   Reply With Quote
Old 23-03-2006, 05:35 PM   #13 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
Ive just noticed that if I put some text into the text file and then upload it, whenever I run the PHP file, the text gets deleted ?!?!?
CAT4361 is offline   Reply With Quote
Old 24-03-2006, 12:56 PM   #14 (permalink)
Registered User
 
CAT4361's Avatar
 
Join Date: Mar 2006
Posts: 14
Do I need to create a ticket to get write permissions activated for the folder I'm trying to write to?

Regards,
Stephen
CAT4361 is offline   Reply With Quote
Old 24-03-2006, 12:58 PM   #15 (permalink)
Administrator
 
Jacob's Avatar
 
Join Date: May 2003
Posts: 1,299
Yes, you would but I have already updated your permissions for the wwwroot folder.

Regards,

Jacob
Jacob is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:32 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.