0

I have a problem with my project. I tried to upload a video to Picasa using a script from here, but the uploaded video file was larger than 600MB and I got this error:

PHP Fatal error: Allowed memory size of 1497366528 bytes exhausted (tried to allocate 681179924 bytes) in /path/to/file/test/picasa.php on line 119

I tried changing memory_limit = 128MB to memory_limit = 1028MB in php.ini but it's still not working.

This is the function to upload:

function upload_file( $authHeader, $user_id, $album_id, $filename ) {
// Picasa album url
$albumUrl = "https://picasaweb.google.com/data/feed/api/user/$user_id/albumid/$album_id";
// Video file to upload
$imgName = $filename;
// Xml data
$ImgXml =   '<entry xmlns="http://www.w3.org/2005/Atom">
                <title>'.$filename.'</title>
                <summary>'.$filename.'</summary>
                <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/photos/2007#photo"/>
                </entry>';

// Get the file size
$fileSize = filesize($imgName);

// Read the Video file, NOTE : THE ERROR FROM FREAD
$fh = fopen($imgName, 'rb');
$imgData = fread($fh, $fileSize);
fclose($fh);

// Count the file and data lengh
$dataLength = strlen($ImgXml) + $fileSize;

// Data for curl postpields
$data = "";
$data .= "\nMedia multipart posting\n";
$data .= "--P4CpLdIHZpYqNn7\n";
$data .= "Content-Type: application/atom+xml\n\n";
$data .= $ImgXml . "\n";
$data .= "--P4CpLdIHZpYqNn7\n";
$data .= "Content-Type: video/x-matroska\n\n";
$data .= $imgData . "\n";
$data .= "--P4CpLdIHZpYqNn7--";
$header = array(
                'GData-Version:  2', 
                $authHeader, 
                'Content-Type: multipart/related; boundary=P4CpLdIHZpYqNn7;', 
                'Content-Length: ' . strlen($data),
                'MIME-version: 1.0'
                );
$return = "";
// Post the video with the data and header
$ch = curl_init($albumUrl);
$options = array(
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_POST => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_HEADER => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_POSTFIELDS => $data,
                    CURLOPT_HTTPHEADER => $header
                );
curl_setopt_array($ch, $options);
$return = curl_exec($ch);
curl_close($ch);

And I tried replacing

$fh = fopen($imgName, 'rb');
$imgData = fread($fh, $fileSize);
fclose($fh);

with this:

ob_start();
readfile($imgName);
$output = ob_get_contents();
ob_end_clean();

But it's still showing the same error. I tried to run the script via ssh, to no luck. Maybe it's because my VPS only has 2 GB of memory?

Then I read about curl and found --data-binary <data>, I think maybe --data-binary <data> could do this job.

My question is:

Are there alternatives to readfile in php or is there another way to achieve this?

and

Can I upload that video to Picasa using curl as a linux command?

Community
  • 1
  • 1
sifile
  • 3
  • 3
  • Both methods you tried to use are equal (since you are assigned it to a variable). Use CURLFile. http://php.net/manual/en/curlfile.construct.php – Charlotte Dunois Jan 14 '15 at 21:50
  • Thanks for answer my question Charlotte, your mean like this ? `$data .= new CURLFile('video.mkv','video/x-matroska','test_name');` – sifile Jan 14 '15 at 21:59
  • You are near, take a look at the example on linked website. `$curlfile = new CURLFile('video.mkv','video/x-matroska','video.mkv'); $data = array('video' => $curlfile);` – Charlotte Dunois Jan 14 '15 at 22:01
  • But to upload to picasa, the curl post data must contain $data (metadata and video file together) but i think i have an idea, i will try your sugest. and will tell the result – sifile Jan 14 '15 at 22:07
  • thanks Charlotte, i have no idea after runing some test, CURLFile not working, and i try to use my code before, but i try at another server with 30gb of ram, and i shock whit the return messages, picasa return "error 413 , Request Entity Too Large" . hahah, picasa api limit for 100, i read from https://productforums.google.com/forum/#!topic/picasa/QilDPTtop-E , thanks you very much ^_^ – sifile Jan 15 '15 at 01:34

0 Answers0