![]() |
|
|
#1 (permalink) |
|
Registered User
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 |
|
|
|
|
|
#3 (permalink) |
|
Registered User
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'); |
|
|
|
|
|
#4 (permalink) |
|
Administrator
Join Date: May 2003
Posts: 1,299
|
Can you get it to print the directory it is attempting to look at?
__________________
Jacob Colton jacob@catalyst2.com Open a ticket | Knowledgebase | Rate catalyst2 | Review catalyst2 |
|
|
|
|
|
#6 (permalink) |
|
Registered User
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 ...?? |
|
|
|
|
|
#7 (permalink) |
|
Registered User
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. |
|
|
|
|
|
#10 (permalink) |
|
Registered User
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 !!
|
|
|
|
|
|
#11 (permalink) |
|
Administrator
Join Date: May 2003
Posts: 1,299
|
I have updated your permissions, please try that now.
Regards, Jacob
__________________
Jacob Colton jacob@catalyst2.com Open a ticket | Knowledgebase | Rate catalyst2 | Review catalyst2 |
|
|
|
|
|
#12 (permalink) |
|
Registered User
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 |
|
|
|
|
|
#15 (permalink) |
|
Administrator
Join Date: May 2003
Posts: 1,299
|
Yes, you would but I have already updated your permissions for the wwwroot folder.
Regards, Jacob
__________________
Jacob Colton jacob@catalyst2.com Open a ticket | Knowledgebase | Rate catalyst2 | Review catalyst2 |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|