X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=blobdiff_plain;f=upload.php;h=b21af6ffc1d0c9daee027cb1e9c92e9d8f292c6f;hp=2509a6b0709ed054011f2ca1531505767199de98;hb=HEAD;hpb=912bdab05d6262ab07e7ea6664192f97239db3e1 diff --git a/upload.php b/upload.php index 2509a6b..b21af6f 100644 --- a/upload.php +++ b/upload.php @@ -80,11 +80,44 @@ function upload_max_filesize() { } } +# return the extension this path should have WITHOUT the period +function path_or_mime_to_ext($path, $mime = 'text/plain', $default = 'txt') { + $last_dot = strrpos($path, '.'); + if($last_dot === false || $last_dot === 0 || $last_dot === (strlen($path) - 1)) { + # no extension + if(isset($GLOBALS['mime_to_ext'][$mime])) { + return $GLOBALS['mime_to_ext'][$mime]; + } else { + return $default; + } + } else { + $ext = strtolower(substr($path, $last_dot + 1)); + if(isset($GLOBALS['ext_to_ext'][$ext])) { + return $GLOBALS['ext_to_ext'][$ext]; + } + return $ext; + } +} + +# Add or fix extension on path +# This just does string manipulation (ie doesn't move/open/etc any files.) +# Mime type used to generate extension ONLY IF it doesn't have one already. +function path_fix_ext($path, $mime = 'text/plain', $default_ext = '.txt') { + $last_dot = strrpos($path, '.'); + if($last_dot === false || $last_dot === 0) { # no extension + $path .= '.' . path_or_mime_to_ext($path, $mime, $default_ext); + } else { + $basename = substr($path, 0, $last_dot + 1); # keep dot + $path = $basename . path_or_mime_to_ext($path, $mime, $default_ext); + } + + return $path; +} # 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') { +function generate_filename($path, $mime = 'text/plain', $default_ext = '.txt') { # lower case $filename = strtolower($path); @@ -116,23 +149,7 @@ function generate_filename($path, $mime = 'text/plain') { return '_'; } - # 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 { - $filename .= '.bin'; - } - } 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; - } + $filename = path_fix_ext($filename, $mime, $default_ext); return $filename; } @@ -221,7 +238,7 @@ function gif_to_png($filename, $new_filename = 'just change extension') { $new_filename .= '.png'; } - imagemagick_convert($filename, $new_filename, "-colorspace sRGB", 'GIF to PNG conversion'); + imagemagick_convert($filename.'[0]', $new_filename, "-colorspace sRGB", 'GIF to PNG conversion'); unlink($filename); return $new_filename; @@ -393,7 +410,7 @@ function convert_uploaded_image($key, $path, $image_width = 0, $image_height = 0 if($image_width > 0 && $image_height > 0) { $convert_params .= " -geometry ${image_width}x$image_height"; } - imagemagick_convert($tmp_filename, $filename, $convert_params); + imagemagick_convert($tmp_filename.'[0]', $filename, $convert_params); unlink($tmp_filename); list($w, $h) = image_w_h_or_die($filename); $ret = "$filename $w $h";