Error saving Facebook profile image via PHP SDK

Asked

Viewed 84 times

0

Good afternoon!

I am using the Facebook SDK to login to my site, I can return the data, including the link of the profile photo, but I can’t save the image, the following error occurs:

PHP Error was encountered

Severity: Warning

Message: file_put_contents(. /images/profile/image name returned): failed to open stream: No such file or directory

For this I use the Curl library with the file_put_contents function.

$imgUrl = "http://graph.facebook.com/ID FACEBOOK/picture?width=300"; 
$imagename= basename($imgUrl);
if(file_exists('./'.$imagename)){continue;} 
$image = $this->curl->getImg($imgUrl); 
file_put_contents('./imagens/perfil/'.$imagename,$image);

Curl:

function getImg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

NOTE: If using this same code with the url of an image from another location works perfectly.

2 answers

0


When changing the basename() function, which looked for the image name by a name, the image was correctly saved. thus remaining

$imgUrl = "http://graph.facebook.com/ID FACEBOOK/picture?width=300"; 
$imagename = "foto_do_usuario.jpg"
if(file_exists('./'.$imagename)){continue;} 
$image = $this->curl->getImg($imgUrl); 
file_put_contents('./imagens/perfil/'.$imagename,$image);

0

Imagine the following file hierarchy:

/ (raiz do site ou projeto)
  pasta1
    arquivo1.php
  pasta_arquivos
    imagem
  arquivo2.php

Now suppose the file 1.php has the following content:

<?php
require '../arquivo2.php';

And the file 2.php has the following content:

<?php
file_put_contents('./pasta_arquivos/imagem', 'vai funcionar?');

If the file 1.php is executed the file/directory error not found will be shown, because the ./ will be done in relation to file 1.php, not file 2.php (which would work).

This problem can be solved by using the constant DIR (returns the directory of the file being included, either by include or require). Then file content2.php headquarters be changed to:

<?php
file_put_contents(__DIR__ . '/pasta_arquivos/imagem', 'vai funcionar?');
  • I tried to do this but of the same error as I am using the Codeigniter also tested with base_url() and yet the error occurs.

  • I figured out more or less how to solve, as I mentioned in the code I was using basename() to return the name of the photo. the name that returned was 20228632_1500520143337391_4330092013544404463_n.jpg? oh=609e20c8b79053e75b7679d74c3b498c&Oe=59FBE89D, when setting any name to image she saved correctly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.