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!
http
uploaded files are visible, their permission is 0644 as well. Where should I upload the files? is there any specific folder?www-data
and ftp uploaded ones are owned by someone likedrupalist
.