User login

Print name and filename of the top-most file attached to a Drupal post

Searched words: 
Print name and filename of the top-most file attached to a Drupal post enable PHP Input format and get the first file name and insert it here

This is very much a hack, as it puts far too much database loading and PHP logic not even in the theme layer, but worse, embedded in the content itself. A better version might use the theme layer to make the $filename and other such variables available to certain content types with files attached, for instance.

For now... here's the hacky way. But first, some of the research to get us there:

php get first element of array
http://us3.php.net/reset

The files array with which we are dealing:

[files] => Array
(
[2] => stdClass Object
(
[fid] => 2
[uid] => 1
[filename] => scf_dist-6.x-1.0-alpha1.tar.gz
[filepath] => sites/default/files/scf_dist-6.x-1.0-alpha1.tar.gz
[filemime] => application/octet-stream
[filesize] => 19140109
[status] => 1
[timestamp] => 1221850324
[nid] => 2
[vid] => 4
[description] => scf_dist-6.x-1.0-alpha1.tar.gz
[list] => 1
[weight] => 0
)
)

So we have to sort it by weight first to get the first file listed.

Sort an array of objects by an object property in PHP. Basic approach:

<?php
$node
= node_load(arg(1));
$files = $node->files;
function
file_weight_sort($a, $b) {
    if (
$a->weight == $b->weight) {
        return
0;
    }
    return (
$a->weight < $b->weight) ? -1 : 1;
}

usort($files, 'file_weight_sort');
$file = reset($files);
$filename = $file->filename;
?>

Resolution

Here it is, in all it's glory:

<!-- PHP code below gets the first file name and path and prints it as needed -->
<li><kbd>wget <?php

$node

= node_load(arg(1));
function
file_weight_sort($a, $b) {
    if (
$a->weight == $b->weight) {
        return
0;
    }
    return (
$a->weight < $b->weight) ? -1 : 1;
}
$files = $node->files;
usort($files, 'file_weight_sort');
$file = reset($files);
$filename = $file->filename;
$filepath = $file->filepath;
global
$base_url;
print
$base_url;
print
'/';
print
$filepath;
?>
</kbd></li>
<li><kbd>tar -zxvf <?php print $filename; ?></kbd></li>

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.