PHP script can't read file from vfs:// (in memory file system) created by my other PHP script

356 views
Skip to first unread message

Alex Kerr

unread,
Jul 4, 2015, 2:04:11 PM7/4/15
to google-a...@googlegroups.com
Hi,

I've got my main PHP script writing an image file out to vfs:// using the PHP GD lib function imagepng(image, filename)

Then I'm using Facebook's PHP SDK to try and post that. FB's SDK uses Curl internally to read the file, so I simply supply the vfs:// filename to it. I then get back an error saying it can't open the file:

PHP Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'couldn't open file "vfs://root/temp/T199952197c"' in /base/data/home/apps/s~My-App-ID/1.385481947954511984/fb/src/Facebook/HttpClients/FacebookCurlHttpClient.php:150 Stack trace: #0 /base/data/home/apps/s~My-App-ID/1.385481947954511984/fb/src/Facebook/FacebookRequest.php(260): Facebook\HttpClients\FacebookCurlHttpClient->send('https://graph.f...', 'POST', Array)

My first thought was a permissions issue (i.e. FB script not allowed to read file my script created), but checking permissions of my image file in vfs:// - they are set to 0666 and I've tried changing them to both 0644 and 0777 (and confirming they are set) and still get the error...

Any ideas please?

Many thanks!
Alex

Stuart Langley

unread,
Jul 5, 2015, 11:23:35 AM7/5/15
to google-a...@googlegroups.com
Files that you write to vfs are removed at the end of the request. If you need the files to persist between requests you'll need to write or move the files from vfs to gs.

Alex Kerr

unread,
Jul 6, 2015, 11:59:47 AM7/6/15
to google-a...@googlegroups.com
Thanks Stuart. However, I've looked into this and confirmed that's not the problem. At the point the exception is thrown in the FB SDK (this is after CURL has tried to post and returned an error), I added some code in to check the temp file to be posted still exists and can be read (by file_get_contents) and both are true - the file is fine and readable, and permissions are set to 0777 (not sure if necessary or not, but just in case). So the problem is CURL cannot read the file from vfs:// - is this a bug?

I also checked what parameters were being passed to CURL immediately before it executes, and they are as follows (I hardcoded the filename):
Parameters = Array ( [source] => CURLFile Object ( [name] => vfs://root/temp/mytest0001 [mime] => image/png [postname] => ) [message] => [access_token] => My-Token [appsecret_proof] => My-Secret-Proof )

I am generating the temp file's name only once in the code, using: 

$randfname = 'mytest0001'; // Normally a short random string $imfname = sys_get_temp_dir().'/'.$randfname;

As a side note, it didn't make sense that the vfs:// file would be deleted at the point CURL accesses it, because the FB SDK is called to do the post immediately after the file is created, and I assume the call is not asynchronous, so my script must be waiting for that to return and thus not deleting any temp files (confirmed of course, as above).

Any ideas appreciated!

Many thanks,
Alex

Alex Kerr

unread,
Jul 6, 2015, 12:49:40 PM7/6/15
to google-a...@googlegroups.com
Also further to my last reply, tried this all with GCS (accessed via gs:// URL) and it doesn't work either, exactly the same error from CURL. I also tried changing the 'source' field in the CURLFile request to 'url' but that was the same again.

Looks like curl can't read from either vfs or gcs?

I do have curl.so enabled in my php.ini and curl_lite not enabled.

Many thanks, for any help,
Alex

On Sunday, July 5, 2015 at 4:23:35 PM UTC+1, Stuart Langley wrote:

Alex Kerr

unread,
Jul 6, 2015, 1:39:43 PM7/6/15
to google-a...@googlegroups.com
Sorry for a 3rd reply, but just to confirm my code all works fine when the image to be uploaded is a static file uploaded with my app, including if the image is in a subfolder. So it appears curl only has a problem with files on vfs:// or gfs:// (unfortunately this is critical to my app as I need somewhere to store temp files momentarily...)

Thanks


On Sunday, July 5, 2015 at 4:23:35 PM UTC+1, Stuart Langley wrote:

Stuart Langley

unread,
Jul 6, 2015, 9:39:04 PM7/6/15
to google-a...@googlegroups.com
Hi Alex,

I don't think cURL will read a file from a user stream wrapper - so you're kind of stuck.

Can you create the multipart form manually?

Alex Kerr

unread,
Jul 7, 2015, 6:42:38 AM7/7/15
to google-a...@googlegroups.com
Thanks Stuart - OK, just trying to understand this - so the user stream wrapper that Google have implemented is what gives the gs:// or vfs:// access?

So if I create the multipart form manually I could get around that - can you give any tips or pointers how I'd get started with doing that please, not quite sure where to start!

Many thanks,
Alex

Stuart Langley

unread,
Jul 9, 2015, 11:44:31 PM7/9/15
to google-a...@googlegroups.com
Hey Alex,

I'm on vacation, I'll try and work out some sample code for you when I get back.

Cheers
Reply all
Reply to author
Forward
0 new messages