I am trying to read this text file thats located in uploads folder. But not sure what the correct path would be. Ive tried alot of different cominations.
The text file is in this folder
/home/username/public_html/uploads/file.text
The cgi script is in this folder
/home/username/public_html/cgi-bin/script.cgi
#!/usr/bin/perl
open DATA, ("< PATH???? ");
$line1 = <DATA>;
$line2 = <DATA>;
print "$line1 \n";
print "$line2 \n";
I can read the file in browser directly from the server
http://www2.domain.com/username/uploads/file.txt
Related
I am trying to access a text file within a zipped folder to extract a certain information, without actually unzipping the file. I am trying to use Archive::Zip. The directory structure is like Data_stats.zip--> Data_stats/ --> full_data_stats.txt. Now I tried this
use Archive::Zip;
use Archive::Zip::MemberRead;
use File::Basename;
$zip_dir=$ARGV[0];
#name =split("\\.",basename($zip_dir)); ## to get zipped folder name
$dir = Archive::Zip->new("$zip_dir");
$fh = Archive::Zip::MemberRead->new($dir,"$name[0]/full_data_stats.txt"); ##trying to reads the file giving the path and mentioning the specific file name
while (defined($line = $fh->getline()))
{
{print}
}
I see it extracting the folder but not reading in the file !!.
Regards
You are assigning to $line but printing $_; try print $line;
I am currently trying to zip some files with perl. The resulting file is printed, so a user who calls the page which executes the script can download or open the zip file.
Looking at the size of the zip file it seems everything worked ok, but if I try to open the file on the server no contents are shown. If I open the file after downloading it, the archive is invalid.
Here's the code:
my $zip = Archive::Zip->new();
my $i;
foreach $i(#files)
{
my $fh = $zip->addFile("$directoryPath$i") if (-e "$directoryPath$i");
}
my $zipFilePath = "Test.zip";
die 'Cannot create $zip_file_name: $!\n' if $zip->writeToFileNamed("$zipFilePath") != AZ_OK;
open (DLFILE, "<$zipFilePath");
#fileholder = <DLFILE>;
close (DLFILE);
print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$zipFilePath\n\n";
print #fileholder;
Can you please tell me where the error is?
I am running the code using xampp on my local windows machine.
Edit: The same happens when I use
use strict;
use warnings;
use autodie;
Edit: The first problem is solved by ysth, thanks for that. Now the archive is not invalid after downloading, but still no files are shown if I open it, while the zip-file's size seems to be correct.
You are corrupting it here:
open (DLFILE, "<$zipFilePath");
#fileholder = <DLFILE>;
close (DLFILE);
by opening it such that it translates "\r\n" to just "\n".
Try this:
open( DLFILE, '<:raw', $zipFilePath );
I have placed the text file "FilenameKeyword.txt" file in E:/Test folder, in my perl script i am trying to traverse through the folder and am i am trying to find a file with filename which has the string "Keyword" in it, later i have printed the content of that file in my script.
Now i wish do the same thing for the file which is placed inside tar file which is compressed.
Hypothetical File from where i am trying to extract the details:
E:\test.tar.gz
Wanted to know if there are possibility in perl to search and read the file without decompressing /unzipping the hypothetical file.If that is not possible, I shall also allocate some temperory memory to decompress the file , which should deleted after extracting the content from the particular text file.
While Searching in the internet i could it is possible to extract and read the gzip/tar file by using Archive::Extract, being new to Perl - i am really confused on how actually i should make use of it. Could you please help on this....
Input file:FilenameKeyword.txt
Script:
use warnings;
use strict;
my #dirs = ("E:\\Test\\");
my %seen;
while (my $pwd = shift #dirs) {
opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
my #files = readdir(DIR);
closedir(DIR);
foreach my $file (#files)
{
if (-d $file and ($file !~ /^\.\.?$/) and !$seen{$file})
{
$seen{$file} = 1;
push #dirs, "$pwd/$file";
}
next if ($file !~ /Keyword/i);
my $mtime = (stat("$pwd/$file"))[9];
print "$pwd$file";
print "\n";
open (MYFILE, "$pwd$file");
while (my $line = <MYFILE>){
#print $line;
my ($date) = split(/,/,$line,2);
if ($line =~ s!<messageText>(.+?)</messageText>!!is){
print "$1";
}
}
}
}
Output(In test program file is placed under E:\Test):
E:\Test\FilenameKeyword.txt
1311 messages Picked from the Queue.
Looking for help to retrieve the content of the file which is place under
E:\test.tar.gz
Desired Output:
E:\test.tar.gz\FilenameKeyword.txt
1311 messages Picked from the Queue.
I was stuck in using CPAN module, CPAN module didn't work for me as i have oracle 10g enterprise edition in the same machine, due do some software conflict Active state perl was unable compile and refer to the perl lib for CPAN module, i have uninstalled oracle in my machine to make this work....
#!/usr/local/bin/perl
use Archive::Tar;
my $tar = Archive::Tar->new;
$tar->read("test.tar.gz");
$tar->extract();
If your file was gzipped only, you could read its contents in a "streamed" manner as outlined here (Piping to/from a child process without system or backtick - gzipped tar files). The article illustrates a technique to use open and a fork to open and decompress the file, and then making it available to Perl's while(), allowing you to iterate over it.
As tar is basically concatenating things, it might be possible to adapt this to your scenario.
I am working on a simple perl program for my first assignment in my programming class. I literally have been stuck on this first part for more than a day. I cannot get my program to simply open a text file that is in the same directory as the program.
#!/usr/bin/perl -w
use strict;
my($fileName, $line);
print "Please enter name of file to be opened: ", "\n";
$fileName = <STDIN>;
chop($fileName);
#Associates FILE filehandle with the file: "filename.txt"
open(FILE, $fileName) or die("Can't open '$fileName': $!");
while(my $line = <FILE>){
print $line;
}
I am using strawberry perl. To run the program I am dragging and dropping the program into the command line to get the address of the program. It then attempts to run it.
It initially gave me a readline on closed filehandle error, and then I included the or die("Can't open '$fileName': $!"); portion of the code.
Now it says that there is no such file at the directory, but I know that the test.txt file is there because I just created it.
Picture of the code results: http://imgur.com/R8s7FFE
File directory that shows locations of my files: http://imgur.com/nUfM4lA)
The prompt is showing C:\Users\jacjar\Documents as the current working directory
So this is where the program will look for test.txt
But it is not in that directory
text.txt is in L:\College\Junior Year\1st Semester\COSC 320 (Programming Languages)
Move your test.txt file to the C: path shown above and it will work
Do you realize you are trying to open C:\User\jacjar\Documents\test.txt?
I have a working perl script that opens a imput file, parse it and then open a output file and write the parsed output to it. Now I want to be able to drop a file to be script. The file should be read and the written file should have the same name with a different extension and be stored in the same directory(!) where the file was dropped from. The script itself is converted to a exe using PAR:Packer. I'm using Windows 7 and the latest version Strawbery Perl (5.16.2)
The way I check for the file name is :
unless ($#ARGV == -1) {
$filename = $ARGV[0];
}
This is how I open the input file :
open (my $mel, "<", $filename) or die "\nFile does not exist.\n";
And this is how I open the output file :
open (my $out, ">", 'majorEventLog.Prs') or die "Can't open output file: $!";
The issue I face is that the input file is not recognised at all. Second, what do I need to do to have the output file to be created in the same directory ? When I created a cmd file the input file worked but it stored the output file in my home directory. But I do not like to use the CMD, and I also would like to have the output file in the same directory than the input file. How do I need to change my code?
Use this to get input file name:
my $filename = shift;
die ("Please provide input file\n") unless defined $filename;
die ("$filename does not exist\n") unless -f $filename;
Use this to generate output filename in the same directory:
use File::Basename;
use File::Spec;
my ($name, $directory, $suffix) = fileparse($filename);
my $outfile = File::Spec->catfile($directory, "output.txt");