1

I want to get the content type name instead of the file name: I have this function that access to the files folder: site/all/default/files and get all the file with a text extension and put the text files in an array, is there a possibilty to retun all content types names that contain text files instead of the text files names?

function get_projects(){

$directory= variable_get('file_directory_path', 'sites/default/files');
$filenames = array();
$extension =".txt";

$iterator = new DirectoryIterator($directory);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
    //check the extension ".txt"
    if (strpos($fileinfo->getFilename(),'.txt') !== false)
    {
        //Delete the extension ".txt"
        $filenames[$fileinfo->getMTime()] =substr($fileinfo->getFilename(), 0, -4);
        //$filenames[$fileinfo->getMTime()] =$fileinfo->getFilename();
        //$fileinfo->getFilename().get
     }
 }
}
 return $filenames;

}

1 Answer 1

2
  function get_content_type_by_filename($filenames = array()) {
    $sql = "SELECT nt.name, fm.fid
    FROM file_managed AS fm
    LEFT OUTER JOIN file_usage AS fu ON (fm.fid = fu.fid)
    LEFT OUTER JOIN node AS n ON (fu.id = n.nid)
    LEFT OUTER JOIN node_type AS nt ON (n.type = nt.type)
    WHERE fu.type = 'node' AND n.nid IS NOT NULL AND fm.filename IN (:filename)";
    $results = db_query($sql, array(':filename' => $filenames));
    $results = $results->fetchAll();
    return $results;
  }

This function accepts an array of file names you want to find content types for and returns an array of matched objects with properties fid and content type.

Update:

Please take a note that I am not trying to give you the exact code rather point you in the right direction.

To achieve what you want, call the function you mentioned in the question (i assume it does what you say, that it returns an array of file names)

so :

function get_content_type_by_filename($filenames = array()) {
    $sql = "SELECT nt.name, fm.fid
    FROM file_managed AS fm
    LEFT OUTER JOIN file_usage AS fu ON (fm.fid = fu.fid)
    LEFT OUTER JOIN node AS n ON (fu.id = n.nid)
    LEFT OUTER JOIN node_type AS nt ON (n.type = nt.type)
    WHERE fu.type = 'node' AND n.nid IS NOT NULL AND fm.filename IN (:filename)";
    $results = db_query($sql, array(':filename' => $filenames));
    $results = $results->fetchAll();
    $types_array = array();
    foreach($results as $obj) {
      $types_array[] = $obj->name;
    }
    return $types_array;
  }
$files_array = get_projects();
$content_types_array = get_content_type_by_filename($files_array);

The $content_types_array will contain the array of content type names. Hope this helps.If this does not give please debug by installing devel module and passing the variables and arrays through dpm() function to see contents of the array.

7
  • have we to give filesnames with extention like file.txt or without? and what is the returned value type? Oct 8, 2013 at 9:35
  • input will be an array of filenames with extension, and return will be an array of objects containing two properties - fid and the content type in which the file was attached
    – Neo
    Oct 8, 2013 at 9:46
  • how to retun only array projects names. I tried but still nothing Oct 8, 2013 at 10:18
  • updated my answer. please have a look.
    – Neo
    Oct 8, 2013 at 10:29
  • 1
    Updated the answer to get you the Content type label.
    – Neo
    Oct 8, 2013 at 10:57

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.