Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:14:18+00:00 2026-05-27T00:14:18+00:00

I would like to upload an image in a custom module. The image must

  • 0

I would like to upload an image in a custom module. The image must be permanently saved and renamed if needed. Later I want to be able to display the image somewhere on my form or wherever I want.

My current code:

function my_module_name_new_form()
{
 ....

$form['icon'] = array
(
    '#type' => 'file',
    '#title' => t('Icon'),
    '#description' => t('Click "Chose File" to select an image to upload.')
);

 ....
}

In the submit hook I have:

// Icon
$filename = $form_state['values']['icon'];
$dest = file_build_uri($filename); 
file_save_data('My data', $dest, FILE_EXISTS_RENAME); 

Nothing happens… I couldn’t find any Drupal 7.x compatible modules either that somehow make life easier here.

Solution:

//----------------------------
// Icon
//----------------------------  
$dest_dir = file_default_scheme() . '://';// Note: file_directory_path() was removed in Drupal 7.x. // $dest_dir contains the destination directory for the file. 

$validators = array('file_validate_extensions' => array('jpg png gif'));

//Save file
if ($file = file_save_upload('icon', $validators, $dest_dir))
{
    $filename = $file->filename;        
    $file_content = file_get_contents($dest_dir.$filename); // Fatal error: Cannot access empty property in C:\xampp\htdocs\drupal\modules\custom\achievements\achievements.module on line 446
}
else
{
    form_set_error('icon', 'Could not upload file.');
}
// To display the image: // die('<IMG SRC="'.file_create_url($dest_dir.$filename).'"/>');
//----------------------------  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:14:19+00:00Added an answer on May 27, 2026 at 12:14 am

    Try:

     $validators = array(
        'file_validate_extensions' => array('jpg png gif'),
      );
      //Save file
      if ($file = file_save_upload('icon', $validators, $your_file_destination)) {
        $file_content = file_get_contents($file->filepath);
      }
      else{
        form_set_error('icon', 'Could not upload file.');
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to upload files just like google mail does. I would want
I would like display something more meaningful that animated gif while users upload file
I'm using CKEditor and would like to be able to allow users to upload
as the title describes i would like to create an image upload with ajax
I want users to be able to upload an image and have it be
In my Rails app I would like to allow users to upload either image
I would like to test the following helper module function: module UploadsHelper def custom_img_tag(upload,
I would like for a user to upload an image to a file in
Hey. I would like to upload a file and then parse it. Because parsing
I've just finished a functional piece of code and would like to upload it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.