0

So i have successfully uploaded my images into my public:/images folder now i want to output/display all of them in page how do i retrieve all the images?

here's code by the way of handling the form.

 function testform($form, &$form_state) {
   $form = array();
   $form['file'] = array(
      '#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;

}

here's the code for validating it.

      function testform_validate($form, &$form_state) {
         $file = file_save_upload('file', array(
        'file_validate_is_image' => array(),
        'file_validate_extensions' => array('png gif jpg jpeg'),
          ));
       if ($file) {
       if ($file = file_move($file, 'public://')) {
       $form_state['values']['file'] = $file;
    }
else {
        form_set_error('file', t('Failed to write the uploaded file the site\'s file   folder.'));
     }
  }
else {
   form_set_error('file', t('No file was uploaded.'));
}

}

here's the code for submitting.

    testform_submit($form, &$form_state) {
         $file=$form_state['values']['file'];
         unset($form_state['values']['file']);
         $file->status = FILE_STATUS_PERMANENT;
         file_save($file);
         drupal_set_message(t('The form has been submitted and the image has been saved,   filename: @filename.', array('@filename' => $file->filename)));
          }
4
  • How did you uploaded the images, via "Files import" menu or via FTP?
    – Kevin Siji
    Jul 29 '14 at 6:06
  • uploaded them using file_save_upload
    – DAY
    Jul 29 '14 at 6:07
  • @DAY I've posted an answer, I used that module it works great for me. Hope it helps! Jul 29 '14 at 6:25
  • thanks for your answer @KrishnaMohan i'll try to get all answers and i'll try them all and see what's best for my site.
    – DAY
    Jul 29 '14 at 7:13
1

you can simply achieve that by using a module called Filebrowser.

Allows site administrators to expose a particular file system folder and all of its subfolders with a fancy FTP-like interface to site visitors. File metainformation (via descript.ion and files.bss) is supported. The module also allows these metafiles to store special content, which can be parsed with a callback. If no callback is specified in the file, only the description will be fetched.

Using this module you can display all your exported files.

Here is the sample screenshot from project page. enter image description here

Hope this post helps you.

1

Firstly get fid of the images and then put it in the function below Like i did:-

$file_load=file_load($fid);  
$url=$file_load->uri;

$url retrieve you image path where you have uploaded your image. Like:- public:/images/image_name

Use this fuction to display your image. image_style

$variables= array(
'style_name' => 'style_name', 
'path' => $url, 
'getsize' => FALSE, 
'attributes' => array(
 'width' => '100',
 'height' => '100')
);

echo theme('image_style',$variables);

Note :-if you are using drupal theme('image_style',$variables) function to display image.then put url like public:/images/image_name in path given above.else you are using to display image then use below code.

Or You can also do this.

$file_url=file_create_url($url); 
echo '<img src="'.$file_url.'"'>;

Hope this will help you.

5
  • do i have to change the uri to public://?
    – DAY
    Jul 29 '14 at 7:08
  • I have write a note in above answer...please check... Jul 29 '14 at 8:12
  • i used theme('image_style',$variables) but it is not showing the images only a box. followed your instructions
    – DAY
    Jul 29 '14 at 9:16
  • Have you put style name in place of style_name. Jul 30 '14 at 3:58
  • firstly put any valid static fid and then check it Jul 30 '14 at 3:59
0

To display all the files managed by Drupal, you can use Views.

Create a view based on files. You can add the fields related to files, i.e. Filename, rendered file, path, etc.

An export of a sample view below:

$view = new view();
$view->name = 'test_files';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'file_managed';
$view->human_name = 'Test Files';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: File: Rendered File */
$handler->display->display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['table'] = 'views_entity_file';
$handler->display->display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['label'] = '';
$handler->display->display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['link_to_entity'] = 1;
$handler->display->display_options['fields']['rendered_entity']['display'] = 'view';
$handler->display->display_options['fields']['rendered_entity']['view_mode'] = 'full';
$handler->display->display_options['fields']['rendered_entity']['bypass_access'] = 0;
/* Field: File: Name */
$handler->display->display_options['fields']['filename']['id'] = 'filename';
$handler->display->display_options['fields']['filename']['table'] = 'file_managed';
$handler->display->display_options['fields']['filename']['field'] = 'filename';
$handler->display->display_options['fields']['filename']['label'] = '';
$handler->display->display_options['fields']['filename']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['filename']['alter']['ellipsis'] = FALSE;
$translatables['test_files'] = array(
  t('Master'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Items per page'),
  t('- All -'),
  t('Offset'),
  t('« first'),
  t('‹ previous'),
  t('next ›'),
  t('last »'),
);
0

You can use media module, to import all the media contents you have uploaded into your site.

  1. First download and install the module.

  2. Now, navigate to

    Content » Media

    example.com/admin/content/media

enter image description here

Here, you can add your new files to media or simply import from your directory location.

enter image description here

Usage:

You will need to add a field to your content type with the widget type "Media file selector", this will bring up the your media selecting window.

enter image description here

Demo video

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.