Hi Guys,

Need some help with an SQL statement. What I would like to is:

1. Check to see if a node has an attachment.

2. If the node does have an attached file, Return the filepath and filename in such a way I can assign a variable to each.

As far as I can see from the upload.module. this is the sQL that checks and loads the node attachments. What I need help with is the end of this snippet...i.e. after building the list of attachments..I would like to assign those to variables....i.e.

$att1="/full/file/path/and/filename.one"
$att2="/full/file/path/and/filename.two" etc.

But my php skills are limited and I'm not sure how to do that.

Anyone got any tips?

if ($node->files && user_access('view uploaded files')) {
        $header = array(t('Attachment'), t('Size'));
        $rows = array();
        $previews = array();

        // Build list of attached files
        foreach ($node->files as $file) {
          if ($file->list) {
            $rows[] = array(
              '<a href="/'. ($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))) . '">'. $file->filename .'</a>',
              format_size($file->filesize)
            );
            // We save the list of files still in preview for later
            if (!$file->fid) {
              $previews[] = $file;
            }
}

Dub..

Comments

kbahey’s picture

Do you know the node ID? If you do then this SQL should do it, assuming $nid has the nodeid you are checking:

Here is function that returns an array containing all the files attached to a node, otherwise, it returns an empty array.



function get_attachment($nid) {
 $files=array();

 $sql = "SELECT filepath FROM {files} WHERE nid = $nid";

 $result = db_query( $sql );
 while ($row = db_fetch_object($result)) {
  if ( $row->filepath )  {
    $files[] = $row->filepath;
  }
 }

 return $files;
}

--
Consulting: 2bits.com
Personal: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba

Dublin Drupaller’s picture

Thanks KB...

Appreciate the heads up...what I need to do is return the node attachement array as strings..

i.e. when it returns the $fields in your snippet above..how do I assign those fields to a variable like the following?

$att1 = "path/filenameone1.txt";
$att1_filename ="filenameone1.txt";
$att2 = "path/filenametwo2.txt";
$att2_filename ="filenametwo2.txt";
...........etc.

Apologies in advance if that is a stupid question..but, I'm fairly new to php.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kbahey’s picture

Here is the function modified to get back teh file name and the attachment.

Since a node can have more than one attachment, an array is the best way to do that.

function get_attachment($nid) {
  $files=array();
  $sql = "SELECT filename, filepath FROM {files} WHERE nid = $nid";
  $result = db_query( $sql ); 
  while ($row = db_fetch_object($result)) {
    $files[] = array('filename' => $row->filename, 
      'filepath' => $row->filepath);
  }
  return $files;
}

You can use it like this:


$nid = your nid;

$att = get_attachment($nid);
for ($i=0;$i<count($att);$i++) {
  $filename = $att['filename'][$i];
  $filepath  = $att['filepath'][$i];
  // Do something with them here
}

So you get $filename and $filepath for each attachment.

--
Consulting: 2bits.com
Personal: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba

Dublin Drupaller’s picture

Thanks for the heads up...appreciate the help..

Quick questions...

(1) can I call that get_attachment function from another module or do I have to paste the full function into the module I'm working with?

(2) The second snippet isn't clear to me (bear with me, I'm very new to php!) will that give me an output like this?:

$filename_1 = "file1.txt";
$filepath_1 = "/files/files1.txt";
$filename_2 = "file2.txt";
$filepath_2 = "/files/files3.txt";
$filename_3 = "file3.txt";
$filepath_3 = "/files/files3.txt";

And so on....

Sorry if that is a stupid question..but, am very new to this stuff..

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kbahey’s picture

(1) can I call that get_attachment function from another module or do I have to paste the full function into the module I'm working with?

Since you are developing a module, better include it in it. Name it _get_attachment() or yourmodule_get_attachment()

(2) The second snippet isn't clear to me (bear with me, I'm very new to php!) will that give me an output like this?:

$filename_1 = "file1.txt";
$filepath_1 = "/files/files1.txt";
$filename_2 = "file2.txt";
$filepath_2 = "/files/files3.txt";
$filename_3 = "file3.txt";
$filepath_3 = "/files/files3.txt";

And so on....

That is the whole idea: NOT to do it like you did. Since we do not know how many attachments we have, we use an array. Suppose you declare 5 of these, but the node has 6 or 7? Your module will be wrong then.

An array will always contain the exact number of attachments, no matter how many you have. count($att) will contain the number of attachments.

Instead if $filename_1 and $filepath_1 you get $att['filename'][0] and $att['filepath'][0], instead of _2 you get [1], ...etc.

Read up on www.php.net on arrays a bit. They are a very convenient and powerful construct.

--
Consulting: 2bits.com
Personal: Baheyeldin.com

--
Drupal performance tuning and optimization, hosting, development, and consulting: 2bits.com, Inc. and Twitter at: @2bits
Personal blog: Ba