JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added image support to metaform and fixed upload.php
authorJason Woofenden <jason183@herkamire.com>
Sat, 20 Jan 2007 08:59:41 +0000 (03:59 -0500)
committerJason Woofenden <jason183@herkamire.com>
Sat, 20 Jan 2007 08:59:41 +0000 (03:59 -0500)
metaform.php
metaform/template.html
metaform/template.php
upload.php

index 9fe48c9..52a41fc 100644 (file)
@@ -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 <head> and <body> 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;
                        }
index fa10571..85dd4d6 100644 (file)
@@ -12,10 +12,10 @@ td.caption { text-align: right; vertical-align: top; font-weight: bold; }
 <!--~~form start~~--><!--~form start~-->
   <h2>~form_name~ entry form</h2>
 
-  <form action="~form_name~.php" method="post"><!--~~editing start~~--><input type="hidden" name="~form_name~_edit_id" value="~~~form_name~_edit_id.attr~~" /><!--~~end~~-->
+  <form action="~form_name~.php~enctype_attr~" method="post"><!--~~editing start~~--><input type="hidden" name="~form_name~_edit_id" value="~~~form_name~_edit_id.attr~~" /><!--~~end~~--><!--~uploads start~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~" /><!--~end~-->
     <table cellspacing="0" cellpadding="4" border="0" summary=""><!--~row start~-->
 
-      <tr><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
+      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
 
     </table>
   </form>
index d6760cf..5a5c0ee 100644 (file)
@@ -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";
-
+<!--~upload_settings start~-->
+# 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/';
+<!--~end~-->
 # 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 <a href="ht
 require_once('code/wfpl/template.php');
 require_once('code/wfpl/format.php');
 require_once('code/wfpl/email.php');
-require_once('code/wfpl/db.php');
+require_once('code/wfpl/db.php');<!--~image_include start~-->
+require_once('code/wfpl/upload.php');<!--~end~-->
 
 function ~form_name~_get_fields() {
        $fields = array();
        <!--~formats start~-->
-       $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~-->
+       $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
+       $~name~ = save_uploaded_image('~name~', $GLOBALS['upload_directory']);<!--~end~-->
        <!--~tem_sets start~-->
        tem_set('~name~', $~name~);<!--~end~-->
 
@@ -53,7 +59,12 @@ function ~form_name~() {
                        # see: code/wfpl/examples/db_connect.php
                        if(file_exists('db_connect.php')) {
                                require_once('db_connect.php');
-                               if($edit_id) {
+                               if($edit_id) {<!--~image_db start~-->
+                                       # uploading nothing means leaving it as is.
+                                       if(!$~name~ && $delete_~name~ != 'Yes') {
+                                               $~name~ = db_get_value('~form_name~', '~name~', 'id = %"', $edit_id);
+                                       }
+                                       <!--~end~-->
                                        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');
-       }
+       }<!--~upload_max start~-->
+
+       tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
 
        tem_sub('form');
 }
index 47019a6..b8762b7 100644 (file)
 #  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 <input
+# type="file" name="foo"> control.
+#
+# ########
+# # HTML #
+# ########
+# 
+# First, your <form> 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:
+# 
+# <form action="foo.php" enctype="multipart/form-data" method="post">
+# <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
+# <input type="file" name="photo" />
+# <input type="submit" name="save" value="Save" />
+# </form>
+# 
+# #######
+# # 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 <input type="file"/> 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.
 #
-# <input type="image" name="photo">
-# 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);
        }
 }