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 under the
8 # terms of the GNU Lesser General Public License as published by the Free
9 # Software Foundation; either version 2.1 of the License, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # This file contains functions to accept files being uplodad with the <input
23 # type="file" name="foo"> control.
29 # First, your <form> tag must contain this attribute:
30 # enctype="multipart/form-data"
32 # Second, you should indicate to the browser the maximum file size (in bytes)
33 # allowed for uploads with a hidden input field named MAX_FILE_SIZE. You can
34 # use the function upload_max_filesize() to get the maximum allowed size that
39 # <form action="foo.php" enctype="multipart/form-data" method="post">
40 # <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
41 # <input type="file" name="photo" />
42 # <input type="submit" name="save" value="Save" />
49 # In the php code you can use either save_uploaded_file('photo',
50 # 'upload/dir/'); or save_uploaded_image('photo', 'upload/dir/'); The only
51 # difference being that save_uploaded_image() will convert gifs to PNGs.
53 # Both functions will generate a reasonable filename based on the filename
54 # passed from the browser (and on the mime-type if there's no extension) unless
55 # you specify a filename. See the comments above the function definitions below
58 # In a future version of save_uploaded_image(), when you specify a filename, it
59 # will check the image type of the uploaded image, and if it's different than
60 # the type you specified, it will convert the image for you.
64 $GLOBALS['mime_to_ext'] = array(
65 'text/plain' => 'txt',
66 'text/html' => 'html',
67 'image/jpeg' => 'jpg',
72 'application/pdf' => 'pdf'
75 $GLOBALS['ext_to_ext'] = array(
82 # return the upload_max_filesize in bytes
83 function upload_max_filesize() {
84 $max = ini_get('upload_max_filesize');
85 $postfix = strtolower(substr($max, -1));
87 return substr($max, 0, -1) * 1073741824;
88 } elseif($postfix == 'm') {
89 return substr($max, 0, -1) * 1048576;
90 } elseif ($postfix == 'k') {
91 return substr($max, 0, -1) * 1024;
98 # pass in the client's path that came from an html <input type="file"/> tag
100 # mime time used to generate extension ONLY IF it doesn't have one already.
101 function generate_filename($path, $mime = 'text/plain') {
103 $filename = strtolower($path);
105 # remove directories (unix, windows and mac paths)
106 $last = strrpos($filename, '/');
107 if($last === false) {
108 $last = strrpos($filename, '\\');
110 if($last === false) {
111 $last = strrpos($filename, ':');
114 $filename = substr($filename, $last + 1);
117 # replace symbols with underscores
118 $filename = ereg_replace('[^a-z0-9_.]', '_', $filename);
120 # remove dots from the beginning (no invisible files)
121 $filename = ereg_replace('^\.*', '', $filename);
124 $last_dot = strrpos($filename, '.');
125 if($last_dot === false) {
127 if(isset($GLOBALS['mime_to_ext'][$mime])) {
128 $filename .= '.' . $GLOBALS['mime_to_ext'][$mime];
131 $basename = substr($filename, 0, $last_dot);
132 $ext = substr($filename, $last_dot + 1);
133 if(isset($GLOBALS['ext_to_ext'][$ext])) {
134 $ext .= $GLOBALS['ext_to_ext'][$ext];
136 $filename = $basename . '.' . $ext;
143 # Move uploaded file, and return the new filename.
145 # Pass in the index into the $_FILES array (the name of the html input tag) and
146 # the path to the folder you'd like it saved to. If path ends with a slash this
147 # function will generate a filename based on the client's name, otherwise it'll
148 # name the file that.
150 # example: save_uploaded_file('pdf', 'uploaded_pdfs/');
151 # example: save_uploaded_file('resume', "/www/example.com/remumes/$user_id.txt");
152 function save_uploaded_file($key, $path) {
153 if(substr($path, -1) == '/') {
154 $filename = $path . generate_filename($_FILES[$key]['name'], $_FILES[$key]['type']);
159 if(!move_uploaded_file($_FILES[$key]['tmp_name'], $filename)) {
160 die('file upload failed');
167 # returns new filename with .png extension
168 function gif_to_png($filename, $new_filename = 'just change extension') {
169 if($new_filename == 'just change extension') {
170 $new_filename = $filename;
171 $last_dot = strrpos($new_filename, '.');
172 if($last_dot !== false) {
173 $new_filename = substr($new_filename, 0, $last_dot);
175 $new_filename .= '.png';
178 $convert = '/usr/local/bin/convert';
179 if(!file_exists($convert)) {
180 $convert = '/usr/bin/convert';
182 if(!file_exists($convert)) {
183 $convert = `which convert`;
185 if($convert == '' || !file_exists($convert)) {
186 die("can't find imagemagick's 'convert' program");
189 $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
191 exec($command, $dummy, $ret);
193 die("image conversion failed. convert did exit($ret)");
196 return $new_filename;
199 # like save_uploaded_file() (above) except it converts gifs to pngs.
201 # FIXME: if a filename is passed in the end of path, we should check if the file type matches, and if not run convert.
202 function save_uploaded_image($key, $path) {
203 if(substr($path, -1) == '/') {
204 $filename = save_uploaded_file($key, $path);
205 if(substr($filename, -4) == '.gif') {
206 $filename = gif_to_png($filename);
210 return save_uploaded_file($key, $path);