'txt',
'text/html' => 'html',
'image/jpeg' => 'jpg',
'image/jpg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png',
'application/pdf' => 'pdf'
);
$GLOBALS['ext_to_ext'] = array(
'text' => 'txt',
'jpe' => 'jpg',
'jpeg' => 'jpg',
'htm' => 'html'
);
# pass in the client's path that came from an html tag
#
# mime time used to generate extension ONLY IF it doesn't have one already.
function generate_filename($path, $mime = 'text/plain') {
# lower case
$filename = strtolower($path)
# remove directories (unix, windows and mac paths)
$last = strrpos($filename, '/');
if($last === false) {
$last = strrpos($filename, '\\');
}
if($last === false) {
$last = strrpos($filename, ':');
}
if($last) {
$filename = substr($filename, $last + 1);
}
# remove dots from the begning (no invisible files)
$filename = ereg_replace('^\.*', '', $filename);
# fix extension
$last_dot = strrpos($filename, '.');
if($last_dot === false) {
#no extension
if(isset($GLOBALS['mime_to_ext'][$mime])) {
$filename .= '.' . $GLOBALS['mime_to_ext'][$mime];
}
} else {
$basename = substr($filename, 0, $last_dot);
$ext = substr($filename, $last_dot + 1);
if(isset($GLOBALS['ext_to_ext'][$ext])) {
$ext .= $GLOBALS['ext_to_ext'][$ext];
}
$filename = $basename . '.' . $ext;
}
}
# Move to save folder, and return new filename.
#
# Pass in the index into the $_FILES array (the name of the html input tag) and
# the path to the folder you'd like it saved to. If path ends with a slash this
# function will generate a filename based on the client's name, otherwise it'll
# name the file that.
#
#
# example: save_uploaded_image('photo', '/www/example.com/images/');
# example: save_uploaded_image('photo', '/www/example.com/images/example.jpg');
function save_uploaded_file($key, $path) {
if(substr($path, -1) == '/') {
$filename = $path . generate_filename($_FILES[$key]['name'], $_FILES[$key]['type']);
} else {
$filename = $path;
}
if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) {
die('file upload failed');
}
}
# returns new filename with .png extension
function gif_to_png($filename, $new_filename = 'just change extension') {
if($new_filename == 'just change extension') {
$last_dot = strrpos($filename, '.');
if($last_dot !== false) {
$new_filename = substr($filename, 0, $last_dot);
}
$new_filename = $filename . '.png';
$newfilename = $filename;
$filename = substr($filename, 0 -4) . '.png';
}
$convert = '/usr/local/bin/convert';
if(!file_exists($convert)) {
$convert = '/usr/bin/convert';
}
if(!file_exists($convert)) {
$convert = `which convert`;
}
if(!file_exists($convert)) {
die("can't find imagemagick's 'convert' program");
}
$command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
exec($command, null, $ret);
if($ret != 0) {
die("image conversion failed. convert did exit($ret)");
}
unlink($filename);
return $new_filename;
}
# like save_uploaded_file except it converts gifs to pngs.
#
# FIXME: if destination has an extension, it should convert to that type.
function save_uploaded_image($key, $path) {
if(substr($path, -1) != '/') {
$filename = save_uploaded_file($key, $path);
if(substr($filename, -4) == '.gif') {
$filename = gif_to_png($filename);
}
return $filename;
} else {
return save_file_upload($key, $path);
}
}
?>