Database Reference
In-Depth Information
# in the filesystem. (Normally, you'd store images only in one
# place or another; this script demonstrates how to do both.)
use
strict
;
use
warnings
;
use
Fcntl
;
# for O_RDONLY, O_WRONLY, O_CREAT
use
FileHandle
;
use
Cookbook
;
# Default image storage directory and pathname separator
# *** (CHANGE THESE AS NECESSARY) ***
# The location should NOT be within the web server document tree
my
$image_dir
=
"/usr/local/lib/mcb/images"
;
my
$path_sep
=
"/"
;
# Reset directory and pathname separator for Windows/DOS
if
(
$^O
=~
/^MSWin/i
||
$^O
=~
/^dos/
)
{
$image_dir
=
"C:\\mcb\\images"
;
$path_sep
=
"\\"
;
}
-
d
$image_dir
or
die
"$0: image directory ($image_dir)\ndoes not exist\n"
;
# Print help message if script was not invoked properly
(
@ARGV
==
2
||
@ARGV
==
3
)
or
die
<<USAGE_MESSAGE;
Usage: $0 image_file mime_type [image_name]
image_file = name of the image file to store
mime_time = the image MIME type (e.g., image/jpeg or image/png)
image_name = alternate name to give the image
image_name is optional; if not specified, the default is the
image file basename.
USAGE_MESSAGE
my
$file_name
=
shift
(
@ARGV
);
# image filename
my
$mime_type
=
shift
(
@ARGV
);
# image MIME type
my
$image_name
=
shift
(
@ARGV
);
# image name (optional)
# if image name was not specified, use filename basename
# (permit either / or \ as separator)
(
$image_name
=
$file_name
)
=~
s
|.*
[
/\\
]
||
unless
defined
(
$image_name
);
my
$fh
=
new
FileHandle
;
my
(
$size
,
$data
);
sysopen
(
$fh
,
$file_name
,
O_RDONLY
)
or
die
"Cannot read $file_name: $!\n"
;
binmode
(
$fh
);
# helpful for binary data
$size
=
(
stat
(
$fh
))[
7
];