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)?


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

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.