[development] D7 File API

Randy Fay randy at randyfay.com
Mon May 2 12:27:45 UTC 2011


The managed_files table will store the link to the images for you. You just
use the managed files api. Take a look at a core (filefield) or contrib
module for examples.

I'm no expert on this, just spent some time cleaning it up in the late D7
phases...

-Randy

On Mon, May 2, 2011 at 12:14 AM, Peter Anderson <list at panda.id.au> wrote:

>  Hi Randy,
>
> I had a look at the File API handbook you linked to, and I guess
> file_usage_add() is what I need, however I tried implementing it and no new
> records are showing in the {file_usage} table...
> Here's the code: file_usage_add($uploaded_file, 'block_icons', 'block',
> $bid);
> (I got $bid from the {block} table)
>
> My module allows users to upload an image to each block on the site, so I
> created my own database table where each block that has an image associated
> with it is listed.
> If I don't need to store the path to the images anymore, I'm assuming
> file_usage_add() does that for me somehow...?
>
>
> On 02/05/11 13:13, Randy Fay wrote:
>
> I'm a bit baffled why you'd consider accessing the database directly - you
> should be using the file API.
>
> File API handbook section is at http://drupal.org/node/555118
>
> You should use the File API to create managed or unmanaged file entries,
> and not worry more about how they are stored in the db.
>
> A module that might help you is
> http://drupal.org/project/filefield_sources. In fact, it might already do
> everything you need. Anyway, it allows you to add a file from any source.
>
> It *is* complicated trying to allow access to a file that is not in your
> public or private files section. The idea of having drupal:// didn't get in.
>
> -Randy
>
> On Sun, May 1, 2011 at 8:21 PM, Peter Anderson <list at panda.id.au> wrote:
>
>>  Hi everyone,
>>
>> I'm upgrading my D6 module to D7 and am trying to work out the best way to
>> implement the new File API...
>> My module allows users to either upload a file from their computer, or
>> enter the relative path of a file already on the server (e.g.
>> /sites/default/themes/...). The relative path to this file is then stored in
>> the DB.
>>
>> I'm not sure what to store in the database in the D7 version: the file URI
>> (public://my_file.png) or the relative path as per the D6 version
>> (/sites/default/files/my_file.png).
>> The reasons I'm not sure are as follows:
>> - Storing the File URI seems to be the obvious choice as that seems to be
>> the default now
>> - But how would I allow users to enter the path of a file already on the
>> server in, say, their theme folder?
>> - Storing the relative path is how I used to do it, so is easier
>> - But then how do I change a relative path to a File URI when required
>> (e.g. by the image_style_url() function)?
>>
>> Any assistance or advice would be much appreciated!
>>
>> --
>> Kind regards,
>> Peter Anderson.http://panda.id.au
>>
>>
>
>
> --
> Randy Fay
> Drupal Module and Site Development
> randy at randyfay.com
> +1  970.462.7450
>
>
> --
> Kind regards,
> Peter Anderson.http://panda.id.au
>
>


-- 
Randy Fay
Drupal Module and Site Development
randy at randyfay.com
+1  970.462.7450
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110502/6558fba9/attachment.html 


More information about the development mailing list