From: Jason Woofenden Date: Sat, 20 Jan 2007 08:59:41 +0000 (-0500) Subject: added image support to metaform and fixed upload.php X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=c4e3ec54f995df5a6db8c497fb826f0d21d35cbc added image support to metaform and fixed upload.php --- diff --git a/metaform.php b/metaform.php index 9fe48c9..52a41fc 100644 --- a/metaform.php +++ b/metaform.php @@ -42,6 +42,8 @@ $GLOBALS['types'] = array( 'pulldown' => array('pulldown', 'options', 'int'), 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'), 'yesno' => array('checkbox', 'yesno', 'varchar(3)'), + 'delete' => array('checkbox', 'yesno', 'n/a'), + 'image' => array('image', 'oneline', 'varchar(200)'), 'submit' => array('submit', 'oneline', 'n/a') ); @@ -105,6 +107,7 @@ function get_fields() { return $ret; } +# this one, that you're using to create forms function set_form_action() { $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']); if($action == '') $action = './'; @@ -149,6 +152,7 @@ function view_sql() { # pass false if you want to exclude the and tag etc. function make_template($whole_file = true) { + $uploads_output_already = false; $tem = new tem(); $tem->load('code/wfpl/metaform/template.html'); $tem->set('form_name', $GLOBALS['form_name']); @@ -161,6 +165,11 @@ function make_template($whole_file = true) { if($input != 'hidden') { $tem->sub('row'); } + if($input == 'image' && !$uploads_output_already) { + $tem->sub('uploads'); + $tem->set('enctype_attr', '" enctype="multipart/form-data'); + $uploads_output_already = true; + } } $tem->set('name', 'save'); $tem->set('caption', 'Save'); @@ -188,6 +197,7 @@ function make_php() { $db_fields = ''; $php_fields = ''; $always_field = false; + $image_included_yet = false; foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; if($input != 'submit') { @@ -200,8 +210,19 @@ function make_php() { if($php_fields != '') $php_fields .= ', '; $php_fields .= '$' . $name; } - $tem->sub('formats'); - $tem->sub('tem_sets'); + if($input == 'image') { + $tem->sub('image_upload'); + $tem->sub('image_db'); + if(!$image_included_yet) { + $tem->sub('image_include'); + $tem->sub('upload_max'); + $tem->sub('upload_settings'); + $image_included_yet = true; + } + } else { + $tem->sub('formats'); + $tem->sub('tem_sets'); + } if(!$always_field and $input != 'checkbox' and $input != 'radio') { $always_field = $name; } diff --git a/metaform/template.html b/metaform/template.html index fa10571..85dd4d6 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -12,10 +12,10 @@ td.caption { text-align: right; vertical-align: top; font-weight: bold; }

~form_name~ entry form

-
+ - +
~caption.html~: ~caption.html~: ~caption.html~: ~caption.html~:
~caption.html~: ~caption.html~: ~caption.html~: ~caption.html~: ~caption.html~:
diff --git a/metaform/template.php b/metaform/template.php index d6760cf..5a5c0ee 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -7,7 +7,11 @@ # To send results by e-mail, all you have to do is set your e-mail address here: $GLOBALS['~form_name~_form_recipient'] = "fixme@example.com"; - + +# Set this to the path to your uploads directory. It can be relative to the +# location of this script. IT MUST END WITH A SLASH +$GLOBALS['upload_directory'] = 'uploads/'; + # To save results to a database, you'll need to create the ~form_name~ table # (the file ~form_name~.sql should help with this), and create a file called # 'db_connect.php' which calls db_connect() see: @@ -17,12 +21,14 @@ if(!file_exists('code/wfpl/template.php')) { die('This form requires db_update('~form_name~', '~db_fields~', ~php_fields~, 'id = %"', $edit_id); tem_set('did', 'updated'); } else { @@ -93,7 +104,9 @@ function ~form_name~() { } else { # form not submitted, you can set default values like so #tem_set('~always_field~', 'Yes'); - } + } + + tem_set('upload_max_filesize', upload_max_filesize()); tem_sub('form'); } diff --git a/upload.php b/upload.php index 47019a6..b8762b7 100644 --- a/upload.php +++ b/upload.php @@ -19,6 +19,49 @@ # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA. + +# This file contains functions to accept files being uplodad with the control. +# +# ######## +# # HTML # +# ######## +# +# First, your
tag must contain this attribute: +# enctype="multipart/form-data" +# +# Second, you should indicate to the browser the maximum file size (in bytes) +# allowed for uploads with a hidden input field named MAX_FILE_SIZE. You can +# use the function upload_max_filesize() to get the maximum allowed size that +# PHP will accept. +# +# Example: +# +# +# +# +# +#
+# +# ####### +# # PHP # +# ####### +# +# In the php code you can use either save_uploaded_file('photo', +# 'upload/dir/'); or save_uploaded_image('photo', 'upload/dir/'); The only +# difference being that save_uploaded_image() will convert gifs to PNGs. +# +# Both functions will generate a reasonable filename based on the filename +# passed from the browser (and on the mime-type if there's no extension) unless +# you specify a filename. See the comments above the function definitions below +# for more details. +# +# In a future version of save_uploaded_image(), when you specify a filename, it +# will check the image type of the uploaded image, and if it's different than +# the type you specified, it will convert the image for you. + + + $GLOBALS['mime_to_ext'] = array( 'text/plain' => 'txt', 'text/html' => 'html', @@ -36,13 +79,28 @@ $GLOBALS['ext_to_ext'] = array( 'htm' => 'html' ); +# return the upload_max_filesize in bytes +function upload_max_filesize() { + $max = ini_get('upload_max_filesize'); + $postfix = strtolower(substr($max, -1)); + if($postfix == 'g') { + return substr($max, 0, -1) * 1073741824; + } elseif($postfix == 'm') { + return substr($max, 0, -1) * 1048576; + } elseif ($postfix == 'k') { + return substr($max, 0, -1) * 1024; + } else { + return $max; + } +} + # 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) + $filename = strtolower($path); # remove directories (unix, windows and mac paths) $last = strrpos($filename, '/'); @@ -56,6 +114,9 @@ function generate_filename($path, $mime = 'text/plain') { $filename = substr($filename, $last + 1); } + # replace symbols with underscores + $filename = ereg_replace('[^a-z0-9_.]', '_', $filename); + # remove dots from the begning (no invisible files) $filename = ereg_replace('^\.*', '', $filename); @@ -74,21 +135,20 @@ function generate_filename($path, $mime = 'text/plain') { } $filename = $basename . '.' . $ext; } + return $filename; } -# Move to save folder, and return new filename. +# Move uploaded file, and return the 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'); - +# example: save_uploaded_file('pdf', 'uploaded_pdfs/'); +# example: save_uploaded_file('resume', "/www/example.com/remumes/$user_id.txt"); function save_uploaded_file($key, $path) { if(substr($path, -1) == '/') { $filename = $path . generate_filename($_FILES[$key]['name'], $_FILES[$key]['type']); @@ -96,23 +156,23 @@ function save_uploaded_file($key, $path) { $filename = $path; } - if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) { + if(!move_uploaded_file($_FILES[$key]['tmp_name'], $filename)) { die('file upload failed'); } + + return $filename; } # 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, '.'); + $new_filename = $filename; + $last_dot = strrpos($new_filename, '.'); if($last_dot !== false) { - $new_filename = substr($filename, 0, $last_dot); + $new_filename = substr($new_filename, 0, $last_dot); } - $new_filename = $filename . '.png'; - - $newfilename = $filename; - $filename = substr($filename, 0 -4) . '.png'; + $new_filename .= '.png'; } $convert = '/usr/local/bin/convert'; @@ -128,7 +188,7 @@ function gif_to_png($filename, $new_filename = 'just change extension') { $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename); - exec($command, null, $ret); + exec($command, $dummy, $ret); if($ret != 0) { die("image conversion failed. convert did exit($ret)"); } @@ -136,18 +196,18 @@ function gif_to_png($filename, $new_filename = 'just change extension') { return $new_filename; } -# like save_uploaded_file except it converts gifs to pngs. +# like save_uploaded_file() (above) except it converts gifs to pngs. # -# FIXME: if destination has an extension, it should convert to that type. +# FIXME: if a filename is passed in the end of path, we should check if the file type matches, and if not run convert. function save_uploaded_image($key, $path) { - if(substr($path, -1) != '/') { + 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); + return save_uploaded_file($key, $path); } }