0

I installed media module in Drupal 7 to browse the uploaded files on the server. I created a new field called media3 and this is its configuration.

enter image description here

This is the next page :

enter image description here

The problem is when I'm trying to add new content to this content type and select them in the library tab from media module, only the image files that are already uploaded through http protocol are displayed.

If I check the Allowed URI schemes checkbox, nothing will be displayed in the library tab. how do I can browse the ftp uploaded files? where they should be to be visible to media module?

15
  • So what are owner and permission settings on them, versus ones you see available?
    – Mołot
    Apr 11, 2014 at 9:29
  • @Mołot they are 0644
    – M a m a D
    Apr 11, 2014 at 9:30
  • Both ones visible and invisible to libraries mentioned? And what about owner?
    – Mołot
    Apr 11, 2014 at 9:35
  • Only http uploaded files are visible, their permission is 0644 as well. Where should I upload the files? is there any specific folder?
    – M a m a D
    Apr 11, 2014 at 9:39
  • 2
    But what is the owner? I could bet you a dollar that http uploaded files are owned by someone like www-data and ftp uploaded ones are owned by someone like drupalist.
    – Mołot
    Apr 11, 2014 at 9:50

1 Answer 1

1

The media module, like pretty much all of the file related modules on Drupal, relies on the managed files system which involves adding data to the db for files that are uploaded, simply using ftp to place them in the appropriate directory bypasses the "drupal way" and doesn't create these database rows, so the media module doesn't know about the files. I have used this method with other modules but not media:

::I do not claim that this is the best way to handle it, but it has worked for my use cases::

The following are the contents of a custom module I through together and assume a few things, first that your node type is "picture", second that you are just using the default "file" field that comes with drupal, and lastly that only files you are aware of exist in the directory "/sites/default/files/importer":

function MYMODULE_menu(){
    $info['db_pics']=array(
        'title' => 'NCD Callbacks',
        'page callback' => 'db_pics_callback',
        'access callback' => 'pics_is_admin',
    );
    return $info;
}
function pics_is_admin(){
    global $user;
    return ($user->uid==1);
}
function db_pics_callback(){
    $url='./sites/default/files/importer';
    $files=traverse($url);
    foreach($files as $source){
        $node=new stdClass();
        $node->type='picture';
        $node->uid=1;
        $node->status=1;

        //This part is optional and just allowed me to create a node using metadata from the image file
        //Get file info
        $fn=exif_read_data($source);
        if(!isset($fn['DateTime'])){
            $fn['DateTime']=$last_dt;
        }else{
            $last_dt=$fn['DateTime'];
        }

        $node->title=$fn['FileName'];
        $names[]=$node->title;
        $node->created=strtotime($fn['DateTime']);

        //Fetch the actual data from the image file
        $image = file_get_contents($source);

        // Returns the new file object after saving the image to the appropriate Drupal File schema
        $file = file_save_data($image, 'public://'.$fn['FileName'], FILE_EXISTS_RENAME);
        $node->field_image['und'][0]=(array) $file;
        node_save($node);
    }
    return '<ul><li>'.implode('</li><li>',$names).'</li></ul>';
}

//quick and dirty utility function assumes only files you want are in the specified directories
function traverse($url){
    $entries=array();
    $dir=scandir($url);
    foreach($dir as $entry){
        if(in_array($entry,array('.','..'))) continue;
        elseif(is_dir($url.'/'.$entry)){
            $entries=$entries+traverse($url.'/'.$entry);
        }else{
            $entries[]=$url.'/'.$entry;
        }
    }
    return $entries;
}

This is a little different than what you asked for, but I see people asking for this functionality frequently so I wanted to give a complete answer about importing files as nodes, for your specific use case the callback function can be significantly shortened to:

function db_pics_callback(){
    $url='./sites/default/files/importer';
    $files=traverse($url);
    $saved_files=db_query('SELECT filename FROM file_managed')->fetchAll(PDO::FETCH_COLUMN);
    foreach($files as $source){
        $image = file_get_contents($source);
        $fn=substr($source,strrpos($source,'/')+1);
        if(!in_array($fn,$saved_files)){
            file_save_data($image, 'public://'.$fn, FILE_EXISTS_RENAME);
        }
    }
    return 'Finished';
}

That should add all of your files to the managed files database table and allow you to view them in the "Library" tab. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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