Gossamer Forum
Home : General : Perl Programming :

Perl idiot request help mkdir script

Quote Reply
Perl idiot request help mkdir script
PHP bunny here, need perl script to create directory to get owner permissions.

Totally perl ignorant. Stole this script, I get "Premature end of script headers"

Any suggestions? Please and thank you.

===

#!/usr/bin/perl
#
# Need to create directory with same permissions as script (me)
#
use CGI;
use File::Path;
$query = new CGI;
$dir = $query->param("dir");
# test
$dir = "/home/httpd/manscat.com/html/vid/data/test/";
if ( ! -d $dir ) {
mkpath( $dir, 1, 0777 )
or die "Could not make directory '$upload_dir' : $!\n";
}
Quote Reply
Re: [StewardManscat] Perl idiot request help mkdir script In reply to
>>>...to get owner permissions. <<<

Not sure I know what you mean here.

Code:
#!/usr/bin/perl

# define modules...
use CGI;
use File::Path;

# setup CGI.pm stuff...
$IN = new CGI;
$dir = $IN->param("dir");

# define the directory we want to create...
$dir = $IN->param('dir') || "/home/httpd/manscat.com/html/vid/data/test/";

# see if it exists, and if it does, then skip...
# otherwise create it.
if ( ! -d $dir ) {
mkdir($dir) || &error("Could not make directory '$upload_dir' : $!\n");
chmod($dir,0777) || &error("Could not CHMOD. Reason: $!");
}

# this could be where you old code was screwing up.
# if an error was to be displayed in your old script,
# you would have needed to use CGI.pm's
# fatalsToBrowser module to report the error.
sub error {

my $error = $_[0];
print $IN->header();
print "Error: <BR><BR>$error";
exit;

}

This is untested btw. So it may also produce errors (I'm hoping not though)

Cheers

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] Perl idiot request help mkdir script In reply to
Security risk not checking the input directory.
Quote Reply
Re: [StewardManscat] Perl idiot request help mkdir script In reply to
I will use something like it.



use
CGI;
use File::Path;
$query = new CGI;
$dir = $query->param(
"dir");
$dir =
"Test";
if (-d
"$dir" ) {
print
"Directory already Exist";
}
else {
mkdir(
"$dir", 0777);
}

Quote Reply
Re: [zeshan] Perl idiot request help mkdir script In reply to
Thanks to all who replied.

Note the parameters for chmod are in the wrong order. The mode comes first, then the file/dir to change.

I also got a 500 error on my server because, if there is no error in the mkdir or chmod steps, the script doesn't produce any output.

The security risk is ok for me, I am passing in the directory name from a php script. Apparently php scripts create directories as user www.data, which means that I (me the web dude) cannot subsequently delete or rename them using my telnet/ftp account.

Thank goodness for places like this, where we php babes can get in and out of $P(e)rL w:eirdN-ess without actually understanding any of it.

Thanks again