I'm struggling to get a file to upload in Drupal 7.12. I can see in phpinfo(32) that the file did upload, but the tutorial I've been looking at isn't actually uploading. I don't need this to be very robust. What am I doing wrong?

I've been following:
http://api.drupal.org/api/examples/form_example%21form_example_tutorial....
http://api.drupal.org/api/examples/form_example%21form_example_tutorial....

My code is below. Any suggestions on how I can get this working?

function ts_menu(){

     $items['ts_form'] = array(
    'title' => 'File Test', 
    'page callback' => 'ts_file', 
    'access arguments' => array('send location'), 
    'type' => MENU_CALLBACK,
  );
  
  return $items;
  }
  
 
 function ts_file()
 {
  //return 'get ready';
  $h = array();
  return drupal_build_form('file_test_form', $h);

 }
 
 
 
 function file_test_form($form_state) { // from 
  // If you are familiar with how browsers handle files, you know that
  // enctype="multipart/form-data" is required. Drupal takes care of that, so
  // you don't need to include it yourself.

  $form['file'] = array( // From: http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_10/7
    '#type' => 'file', 
    '#title' => t('Image'), 
    '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
  );

  $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'),
  );

  return $form;
}



 function file_test_form_submit($form, &$form_state) { // Form http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_10_submit/7
    $file = file_load($form_state['values']['file']); // Try 1
    print_r($file);
   $file = $form_state['storage']['file'];  // Try 2
  // We are done with the file, remove it from storage.
  unset($form_state['storage']['file']);
  // Make the storage of the file permanent
  $file->status = FILE_STATUS_PERMANENT;
  // Save file status.
  //file_save($file);
  // Set a response to the user.
  drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
 //print_r($form_state);
 exit();

}

Comments

Bagz’s picture

jeditdog’s picture

that example still confuses me because it's Drupal 6 and some of the functions used have been removed or deprecated.

Does anyone have a quick example on how to handle file uploads? Or a lengthy one? I realize there is a lot to understand, but the documentation is pretty much non existent. I'm surprised it's this difficult considering it's fairly easy in Php itself.

jaypan’s picture

I learned how to work with Drupal's file API using the tutorial named 'Working with Files and the File API' which can be purchased here: http://buildamodule.com/

I found it very helpful, it when through both managed and unmanaged files, private and public files, and explained how the stream wrappers work allowing for the public:// and private:// schemas.

Contact me to contract me for D7 -> D10/11 migrations.

Bagz’s picture

Righto. Because I gave you a bum steer, here is a free working example:

function test() {
  $content = drupal_get_form('file_upload_example');
}

function file_upload_example() {
	// show the form
	$form['upload_file'] = array(
		'#title' => t('File to be uploaded'),
		'#type' => 'file',
		'#description' => t('Only files with a .csv or .txt extension are allowed.'),
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Upload',
	);
	return $form;
}


function file_upload_example_validate($form, &$form_state) {
  // do any form validations here
}

function file_upload_example_submit($form, &$form_state) {

  // SAVE THE FILE
  // 1. (optionally) specify valid file extensions
  // 2. save to public:// (or private://) (default is temporary://)
  //    this is your configured file location (usually sites/default/files)
  //    you can add subdirectories if you wish, i.e. public://uploads
  // 3. deal with duplicate filenames by specifying one of:
  //    FILE_EXISTS_REPLACE: Replace the existing file.
  //    FILE_EXISTS_RENAME: Append _{incrementing number} until the filename is unique.
  //    FILE_EXISTS_ERROR: Do nothing and return FALSE.

  $validators = array('file_validate_extensions' => array('csv txt'));
  $file = file_save_upload('upload_file', $validators, 'public://', FILE_EXISTS_REPLACE);

  if ($file) {
    
    // the upload file was saved as a temporary file (status=0)
    // we can  save it as a permanent file so it won't get cleaned up later
    $file->status = 1;
    $result = file_save($file);

    // the file is now available to Drupal
    // or we can process it directly for example:
    $realpath = drupal_realpath($file->uri);
    $fh = fopen($realpath, 'r');

    drupal_set_message("the file $file->filename was uploaded");
  }
  elseif (!isset($file)) {
    drupal_set_message('Please specify a file to be uploaded.', 'error');
  }
}

and yes, this originally took me a while to figure out as well...

jeditdog’s picture

To summarize what happened here. I was able to get the examples working but only in a Linux environment. My windows dev environment was at the heart of the problem.

I'm sure the main problem was with my Apache Setup so if you're having this problem, I suggest trying it on Linux.