Asim Zaidi
Asim Zaidi

Reputation: 28344

file_get_contents expects a string

I am trying to get a file from s3 and move it to my local file system and I am getting this error

file_get_contents() expects parameter 1 to be string, object given

$upload_directory = "C:\wamp\test";

$s3 = new AmazonS3("key", "pass");



$source = $s3->getObject("bucket","file.mp3");
$destination = "C:\wamp\test\file.mp3";

$data = file_get_contents($source);

$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);

Upvotes: 0

Views: 2541

Answers (5)

plusbryan
plusbryan

Reputation: 121

Casting to string won't work for the object returned from $s3->getObject. If you need a string back from $s3->getObject, do this instead:

$image = $s3->getObject($bucket, "file.jpg");
if ($image->body) {
    return $image->body;
} else {
    return '';
}

Upvotes: 0

Ryre
Ryre

Reputation: 6181

Cast the object into a string.

$source = (string) $s3->getObject("bucket","file.mp3");

Upvotes: 0

BRampersad
BRampersad

Reputation: 862

see if the return object has a __toString() function that you can call. If it returns the path of the file, then you can use it in the function file_get_contents.

Upvotes: 0

Phil
Phil

Reputation: 165065

Obviously, AmazonS3::getObject() does not return a string. Does the object returned from getObject() offer a URL string method or property?

Edit: I know this isn't what you're using but amazon-s3-php-class appears to offer a much simpler interface for saving an S3 object locally.

Upvotes: 2

Ibu
Ibu

Reputation: 43850

You are passing it an object apparently, var_dump($source) to see the kind of data you are getting back first. it needs to be a string

Upvotes: 2

Related Questions