1

I want to provide an upload button on a node display page. By clicking a button, it should be possible to upload files. Here is what I'm thinking of:

enter image description here

For this I am changing the node.tpl file.

 <?php //code added 
     if($node->title=='Course Materials'){?>
        <form action="" name="uploadform" >
            <input type="submit" value="upload" name="upload"/>
        </form>
    <?php  }
    if(isset($_REQUEST['upload'])){ ?> 
    <form  method="post" action="" enctype="multipart/form-data">
        <input type="file" name="file"  />
        <input type="submit" name="submit"  />
    </form> 
     <?php //end 
     }?>

And to save the files:

  if(isset($_REQUEST['submit'])){ 
    if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

   //$file = $_FILES['file']['tmp_name'];
   move_uploaded_file($_FILES["file"]["tmp_name"], "C:/xampp/htdocs/drupal6upload/sites/default/files/".$_FILES["file"]["name"]);
    $node = node_load($node->nid);

        // Build the file object
        $file_obj = new stdClass();
        $file_obj->filename = $_FILES['file']['name'];
        **$file_obj->filepath = "C:/xampp/htdocs/drupal6upload/sites/default/files/".$_FILES["file"]["name"];**
        $file_obj->filesize = $_FILES['file']['size'];
        $file_obj->uid = 1;
        $file_obj->status = FILE_STATUS_TEMPORARY;
        $file_obj->timestamp = time();
        $file_obj->list = 1;
        $file_obj->new = true;

        // Save file to files table
        drupal_write_record('files', $file_obj);

        // change file status to permanent
        file_set_status($file_obj,1);

        $node->field_test1[] = (array)$file_obj;
        node_save($node);

        drupal_goto('node/'.$node->nid);
    }
}

But I am facing some problem to set the filepath and file_copy instead of move_uploaded_file.

Any suggestions?

1

I am able to do it by creating custom module.

    function upload_course_form($form_id) {
    $form = array();
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['course_materials'] = array(
            '#type' => 'file',
            '#title' => t('Course Materials:'),
            '#size' => 60,
            );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#weight' => 10
    );
    return $form;
}

  function upload_course_form_validate($form, &$form_state){
  $field = 'course_materials';
  $directory = file_directory_path();
  if (file_check_directory($directory, FILE_CREATE_DIRECTORY, $field)) {
    $validators = array('file_validate_extensions' => array('pdf xls html doc txt docx'));
    if ($file = file_save_upload($field, $validators, $directory)) {
      file_set_status($file, FILE_STATUS_PERMANENT);
      $form_state['storage']['file'] = $file;

    }
  }
}

function upload_course_form_submit(&$form, &$form_state){
print_r($form_state['storage']['file']); //see the upload file details 
//and add the code to save the node.

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.