3 # Copyright (C) 2007 Jason Woofenden
5 # This file is part of wfpl.
7 # wfpl is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with wfpl; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 $GLOBALS['mime_to_ext'] = array(
23 'text/plain' => 'txt',
24 'text/html' => 'html',
25 'image/jpeg' => 'jpg',
29 'application/pdf' => 'pdf'
32 $GLOBALS['ext_to_ext'] = array(
40 # pass in the client's path that came from an html <input type="file"/> tag
42 # mime time used to generate extension ONLY IF it doesn't have one already.
43 function generate_filename($path, $mime = 'text/plain') {
45 $filename = strtolower($path)
47 # remove directories (unix, windows and mac paths)
48 $last = strrpos($filename, '/');
50 $last = strrpos($filename, '\\');
53 $last = strrpos($filename, ':');
56 $filename = substr($filename, $last + 1);
59 # remove dots from the begning (no invisible files)
60 $filename = ereg_replace('^\.*', '', $filename);
63 $last_dot = strrpos($filename, '.');
64 if($last_dot === false) {
66 if(isset($GLOBALS['mime_to_ext'][$mime])) {
67 $filename .= '.' . $GLOBALS['mime_to_ext'][$mime];
70 $basename = substr($filename, 0, $last_dot);
71 $ext = substr($filename, $last_dot + 1);
72 if(isset($GLOBALS['ext_to_ext'][$ext])) {
73 $ext .= $GLOBALS['ext_to_ext'][$ext];
75 $filename = $basename . '.' . $ext;
81 # Move to save folder, and return new filename.
83 # Pass in the index into the $_FILES array (the name of the html input tag) and
84 # the path to the folder you'd like it saved to. If path ends with a slash this
85 # function will generate a filename based on the client's name, otherwise it'll
88 # <input type="image" name="photo">
89 # example: save_uploaded_image('photo', '/www/example.com/images/');
90 # example: save_uploaded_image('photo', '/www/example.com/images/example.jpg');
92 function save_uploaded_file($key, $path) {
93 if(substr($path, -1) == '/') {
94 $filename = $path . generate_filename($_FILES[$key]['name'], $_FILES[$key]['type']);
99 if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) {
100 die('file upload failed');
105 # returns new filename with .png extension
106 function gif_to_png($filename, $new_filename = 'just change extension') {
107 if($new_filename == 'just change extension') {
108 $last_dot = strrpos($filename, '.');
109 if($last_dot !== false) {
110 $new_filename = substr($filename, 0, $last_dot);
112 $new_filename = $filename . '.png';
114 $newfilename = $filename;
115 $filename = substr($filename, 0 -4) . '.png';
118 $convert = '/usr/local/bin/convert';
119 if(!file_exists($convert)) {
120 $convert = '/usr/bin/convert';
122 if(!file_exists($convert)) {
123 $convert = `which convert`;
125 if(!file_exists($convert)) {
126 die("can't find imagemagick's 'convert' program");
129 $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
131 exec($command, null, $ret);
133 die("image conversion failed. convert did exit($ret)");
136 return $new_filename;
139 # like save_uploaded_file except it converts gifs to pngs.
141 # FIXME: if destination has an extension, it should convert to that type.
142 function save_uploaded_image($key, $path) {
143 if(substr($path, -1) != '/') {
144 $filename = save_uploaded_file($key, $path);
145 if(substr($filename, -4) == '.gif') {
146 $filename = gif_to_png($filename);
150 return save_file_upload($key, $path);