CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Check if file is uploaded asci or binary

 



mmcw
User

Apr 11, 2001, 11:26 AM

Post #1 of 7 (992 views)
Check if file is uploaded asci or binary Can't Post

Is it possible to check if a file is uploaded in asci or in binary. I need to make a little script to check if sertain file are uploaded in asci. This check has to be done before I can use my perl script. Wenn a file called test.setup is uploaded in binary the script will fail. I upload the file using a ftp program

greetings Michel



Jasmine
Administrator

Apr 11, 2001, 4:41 PM

Post #2 of 7 (983 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

Use -T to check if it's a text file and -B to check if it's a binary file


Code
my @files = grep {-f} glob '/directory/path/*'; 

for my $file (@files){
if ( -T $file ){
print "$file is text\n";
}
elsif ( -B $file ){
print "$file is binary\n";
}
}



mmcw
User

Apr 12, 2001, 1:27 AM

Post #3 of 7 (976 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

I tried this code:

my $file = "$path/shop.setup";
if (-T $file) {
print "Content-type: text/html\n";
print "\n";
print "$file is text
\n";
}
elsif (-B $file) {
print "Content-type: text/html\n";
print "\n";
print "$file is binary
\n";
exit;
}

This will work when the file shop.setup is a text file.
When I upload it (to test) as a binary file I get the following error:

Illegal character \015 at shop.setup line 6
(Maybe you didn't strip carriage returns after a network transfer?)

The first lines of the shop.setup file look like this:

# shop.setup - setup file for shop.cgi
#
#####
##### DEFINE DIRECTORIES
#####

# Check if neccessary files are available
# like basis.cat and basis.setup. When not available
# This files will be made. Set to "1" to check.
# Set to 1 to get some setup information.

How to make it work?



Jasmine
Administrator

Apr 12, 2001, 9:58 AM

Post #4 of 7 (968 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

Sounds like you're trying to require shop.setup into your program and having it test itself.

Since shop.setup is causing the program itself to fail by being uploaded in binary format instead of ascii format, the program cannot get to the file test. What you can do is wrap the require call in eval and *then* to the test if it fails:


Code
eval{ require "shop.setup" }; 
if ( $@ ){
# require failed. do something here.
# remember that nothing from shop.setup
# can be used.
}



mmcw
User

Apr 12, 2001, 11:17 PM

Post #5 of 7 (960 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

When I use your code it doesn't matter if I upload it in asci or binary!
I thank you a lot!!
Could you tell me why? I am learning perl every day!



mmcw
User

Apr 12, 2001, 11:18 PM

Post #6 of 7 (960 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

Is it possible to use something like this:

$path = "path/to/dir";

eval {
require "$path/shop.setup";
require "$path/lib.pl";
require "$datalib/display_menu.pl";
};

Or do I have to use something else??



Jasmine
Administrator

Apr 13, 2001, 7:15 AM

Post #7 of 7 (953 views)
Re: Check if file is uploaded asci or binary [In reply to] Can't Post

Sure, you can use that, but in order for it to be helpful, do something if the eval fails.


Code
eval {  
require "$path/shop.setup";
require "$path/lib.pl";
require "$datalib/display_menu.pl";
};

if ( $@ ){
print "ERROR: $@\n";
exit;
}

$@ contains the last error from an eval statement. If there were no errors, then nothing will be in $@ and the program will continue merrily on its way.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives