Posted by dman on June 8, 2010 at 6:59pm
I'm working through a module upgrade, and hit a bit of a standstill with the Drupal 7 file wrappers.
Simple case - I use file_save_upload() to upload a file from a form.
It creates a temporary, sorta-virtual file and tells me:
stdClass Object
(
[uid] => 1
[status] => 0
[filename] => faqTerms.rdf
[uri] => temporary://faqTerms.rdf
[filemime] => application/rdf+xml
[filesize] => 1162
[source] => file_upload
[destination] => temporary://faqTerms.rdf
[timestamp] => 1276023082
[fid] => 6
)Um, right.
So how do I read the contents of this uploaded file?
I can't see any read function in the public API, although there is a file_save_data() going the other way.
Is this a gap in the Drupal 7 API? Do I have to do it by hand?
I see in stream_wrappers.inc a declaration of stream_read() ... but where's good old file_get_contents() or equivalent?
For now, it seems I can go
<?php
$file->filepath = drupal_realpath($file->uri);
$data = file_get_contents($file->filepath);
?>... but it feels like that is entirely undermining the point of the stream wrappers.
So what am I missing?
Comments
Take a look at
Take a look at http://api.drupal.org/api/function/hook_file_load/7 there's a whole lot to learn with D7...
Pobster
:-(
I saw file_load which will "Load a file object from the database.", and that's what hook_file_load helps.
.. but that's all just about the file. I can't find the API, or any examples in core, for reading the contents of the file.
I don't think I'm supposed to extend the stream wrapper class or whatever it takes just to get file_get_contents behavior .. but it's looking like that.
.dan. is the New Zealand Drupal Developer working on Government Web Standards
PHP4 style?
Looking for examples, I found that file_managed_file_save_upload() is the closest example I could find to something using the new API. But when that calls file_save_upload() it gives it a specific destination, so it has no problem knowing the filepath.
However in my case I actually really like the
[uri] => temporary://behavior - because I'm just uploading some config to be read and temporary is great for this. Deducing the filepath and reading that is sorta OK, but breaks the stream method.I still don't see any extant example of when the Drupal stream API will actually read a managed file. Does it need to be built?
Should there be a new method called file_get_contents in the wrapper interface to take care of the stream_open(), stream_read(), stream_close() (Can't link to api.d.o) or do we party like it's 1999 and still using PHP4 to do i by hand?
I think I could build the method - for local files anyway ... but I'm feeling I'm not seeing something that should be there.
(Context is taxonomy_xml that accepts file upload of text data that needs to be read into the system)
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks .dan. !
Thanks for workaround, dman. Just stucket at the same problem now... and found this post. Nice!