1

I'm creating an module and there I want to read a text file.

I don't want to save the file.

In my my_module.module file I have

function my_module_upload_form($form, &$form_state) {
    $form['upload'] = array(
        '#type' => 'file',
    );

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

    return $form;
}
function my_module_upload_form_submit($form, &$form_state) {
    $file = file_load($form_state['values']['upload']);
    $url = file_create_url($file->uri);
}

I get the error "Trying to get property of non-object" at

$url = file_create_url($file->uri);

Can someone point me how to read the file correctly (without saving it)?

2

Ok I figured it out myself!

function my_module_upload_form_submit($form, &$form_state) {
    //saved the file into a tmp folder (location to save not set)
    //this will also add a pointer in the database
    $file = file_save_upload('upload', array(
        'file_validate_extensions' => array('txt'),
    ));

    //get the content in the file
    $content = file_get_contents($file->uri);

    //remove the file in temp and the pointer in the database
    file_delete($file);
}
  • Best Answer, Was looking for this. Mine wasn't working because I was saving into my ::public directory. – Sanjok Gurung Jan 16 at 17:14

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.