Gossamer Forum
Home : General : Perl Programming :

Re: [Paul] error on mkdir

Quote Reply
Re: [Paul] error on mkdir
HELP! HELP! HELP! HELP!

I can't find a good CGI tutorial on mkdir's
The ones I do find only show part of the code like:
"mkdir($dir,0777)"
THEY DON' SHOW what's before this (how to query it) or after the code (HOW TO finish the execution).

I use PHP but the SAFE MODE is on so It's not working right.
I downloaded CGI programs that mkdir's on my server but I can't under stand the code.

Here is what I came up with to pars my own form query >>>>

########################################################
$QueryString = $ENV{ 'QUERY_STRING' } ;

@NameValuePairs = split (/&/, $QueryString, 1);

$parent_dir = "/home/user/public_html/folder/";

foreach $NameValue (@NameValuePairs)
{
($Name, $Value) = split (/=/, $NameValue);
$Value =~ tr/+/ /;
mkdir($parent_dir$Value,0777) || die "Cannot mkdir newdir: $!";
}
########################################################

Can you help me out ser with a complete sample of a query to make a directory?
Thanks!

AlexMcleaniii@yahoo.com
Quote Reply
Re: [am3] error on mkdir In reply to
Code:
require Cwd;

my $dir = Cwd::cwd() . '/testing12345';

mkdir($dir) || die "Barfed: $dir. Reason: $!";

Last edited by:

Paul: Aug 28, 2002, 8:44 AM
Quote Reply
Re: [Paul] error on mkdir In reply to
Thanks, now where does this go?

Unsure
Quote Reply
Re: [am3] error on mkdir In reply to
In your perl script perhaps?
Quote Reply
Re: [Paul] error on mkdir In reply to
I listed the code I was having a problem with (by the way, I am new to perl)

You sent me this:

#####################
require Cwd;

my $dir = Cwd::cwd() . '/testing12345';

mkdir($dir) or die "Barfed: $dir. Reason: $!";

#####################

Before I test this, doe's $dir grab the value sent from my form field named "dir"? If not how will I pars the form and get it to name & make the new directory.

Thanks
Quote Reply
Re: [am3] error on mkdir In reply to
Hmm I gave you a demo for you to work off.

No offence but the code you have is a little bit out-dated, you should be looking to use CGI.pm for form parsing.

If you are trying to make a directory tree rather than one directory you will probably want to use File::Path to make it easier for yourself. You can find info on File::Path and CGI.pm at perldoc.com or cpan.org
Quote Reply
Re: [am3] error on mkdir In reply to
Btw, how did this thread get split into two?
Quote Reply
Re: [Paul] error on mkdir In reply to
"No offence but the code you have is a little bit out-dated"

No offence taken... I am useing an old book and I also may be using old site tutorials as well.

This is what I did before your reply was posted:


#################
$QueryString = $ENV{ 'QUERY_STRING' } ;

@NameValuePairs = split (/&/, $QueryString, 1);

$parent_dir = "/home/user/public_html/folder/";

foreach $NameValue (@NameValuePairs)
{
($Name, $Value) = split (/=/, $NameValue);
$Value =~ tr/+/ /;
#$Value = ~ s/%([\ dA-Fa-f][\ dA-Fa-f])/ pack ("C", hex ($1))/eg;
#$name =~ s/%(..)/pack("c",hex($1))/ge;
#$Value =~ s/%(..)/pack("c",hex($1))/ge;
my $dir = Cwd::cwd() . "$parent_dir$Value";
mkdir($dir) or die "Barfed: $dir. Reason: $!";
}
################################

This worked (it created my folder) however the bowser gave error massage. It would be nice to see a "dir was creaded" massage.

I can't wait to check out the resources you posted. Thanks.
If there is any thing els that comes to mind about my abjectives, please reply or els thanks so much for your help.
Quote Reply
Re: [am3] error on mkdir In reply to
The temptation to post rather than let you use that code is too great :)

Code:
use File::Path;
use CGI qw/:cgi/;

my $dir = '/the/start/of/your/path';
my $end = param('dir');
my $path = $dir . '/' . $end;

$end =~ /^\w+$/ or die "Invalid directory $end";

unless (-d $path) {
File::Path::mkpath($path) || die "Can't make path $path, $!";
}

print header;
print "Made $path (or perhaps it existed)";

Call like:

your_script.cgi?dir=foobar

Last edited by:

Paul: Aug 28, 2002, 9:56 AM
Quote Reply
Re: [Paul] error on mkdir In reply to
Nice!!!!!!!!!

I spent over 10 hours in my book and on line trying code after code...
Thanks so much Paul for you help.

Alex
Quote Reply
Re: [am3] error on mkdir In reply to
May be worth investing in new book...Learning Perl, by O'Reilly is a good one IMO.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] error on mkdir In reply to
I had in mind to go grab a new book tonight after work.
I'll make sure to check out "Learning Perl, by O'Reilly"

Thanks Andy.
Quote Reply
Re: [Paul] error on mkdir In reply to
One last question...
in your code, where would you set the permission?

Here is the line I think need to change but it does nothing...

File::Path::mkpath($path) || die "Can't make path $path, $!";

to

File::Path::mkpath($path, 0777) || die "Can't make path $path, $!";

This does not work? Unsure
can you help.
By the way I picked up a book (Learn Perl in 24 hours).
I did not look in to the book for this solution just yet.
I just wanted to go back to the source of the code. You!

Thanks
Quote Reply
Re: [am3] error on mkdir In reply to
If you have a fairly new version of perl you can leave out 0777....anyway that is the umask not chmod

Below that line add:

chmod(0777, $path) || die "Can't chmod $path, $!";

Last edited by:

Paul: Aug 31, 2002, 2:17 AM
Quote Reply
Re: [Paul] error on mkdir In reply to
Thanks