Code for a simple form, for upploading images in Drupal

Submitted by Local Tree Child on Thu, 03/29/2007 - 11:17.

Trying to find some code to make it easy for people to upload their favorite picture together with their favorite memory anonymously, I patched together this from various sources. It lets you upload files, pictures in this case, to your choosen directory.

<?php
function render_form() {
   
// Form:
$form['new']['upload'] = array('#type' => 'file', '#title' => t('Upload image'), '#size' => 40);
$form['new']['attach'] = array('#type' => 'submit', '#value' => t('Upload'));
$form['#attributes']['enctype'] = 'multipart/form-data';

$output = drupal_get_form('render_form', $form);
return
$output;
}

print
render_form();
       
function
render_form_submit($form_id, $form_values) {

// Submit hook:
# if ($op == t('Upload')) {   
   
$dir = variable_get('file_directory_path', NULL); //file_create_path('files/badges');
   
$is_writable = file_check_directory($dir, 1);
    if(
$is_writable) { 
     
$source = file_check_upload('upload');     
     
# Security measure to prevent exploit of file.php.png
      # File type permission list
     
$list = 'jpg gif';
     
$source->filename = upload_munge_filename($source->filename,$list);
      
      if (
$file = file_save_upload($source,$dir )) {
        if (
image_get_info($file->filepath)) {
         
drupal_set_message(t('New image saved.'));
        } else {
         
file_delete($file->filepath);
         
drupal_set_message('Uploaded file does not appear to be a valid image file. Please try again.');
        }
      }
    }

 
#}
 
}
?>

urchinTracker(); // -->