Jump to content

A very small problem with uploading image in my script


radiations3

Recommended Posts

I have this code and its working awsomely fine if i provide a direct link of an uploaded image.

Just and just only one problem i am not able to pass my uploaded image to facebook->api (whether its valid or not) and the following always echo

  echo 'Only jpg, png and gif image types are supported!';

i even remove the check on image type and try to get the image from

  $img = realpath($_FILES["pic"]["tmp_name"]);

but $img gets nothing in it and uploads a by default empty image on facebook as my page

Kindly check my following code and let me know what is wrong with my code and what should i do instead to upload images

 

Online link of the following CODE: http://radiations3.com/facebook/1.php

 

 

   

 <?
    
    
    require 'src/facebook.php';
    
    $app_id = "364900470214655";
    $app_secret = "xxxxxxxx";
    
    $facebook = new Facebook(array(
     'appId' => $app_id,
     'secret' => $app_secret,
     'cookie' => true,
     'fileUpload' => true,
    ));
    
    $user = $facebook->getUser();
    //echo $user;
    
    if(($facebook->getUser())==0)
    {
     header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
     exit;
    }
    else {
    $accounts_list = $facebook->api('/me/accounts');
    echo "i am connected";
    }
      $valid_files = array('image/jpeg', 'image/png', 'image/gif');
    
    
    //to get the page access token to post as a page
    foreach($accounts_list['data'] as $account){
          if($account['id'] == 194458563914948){      // my page id =123456789
            $access_token = $account['access_token'];
            echo "<p>Page Access Token: $access_token</p>";
            }
        }
    
    //posting to the page wall
    
    if (isset($_FILES) && !empty($_FILES))
    {  
    if( !in_array($_FILES['pic']['type'], $valid_files ) )
    {
      echo 'Only jpg, png and gif image types are supported!';
     }
     else{
      #Upload photo here
      $img = realpath($_FILES["pic"]["tmp_name"]);
    $attachment = array('message' => 'this is my message',
                    'access_token'  => $access_token,
                    'name' => 'This is my demo Facebook application!',
                    'caption' => "Caption of the Post",
                    'link' => 'example.org',
                    'description' => 'this is a description',
                    'picture' => '@' . $img,
                    'actions' => array(array('name' => 'Get Search',
                                      'link' => 'http://www.google.com'))
                    );
    $status = $facebook->api('/194458563914948/feed', 'POST', $attachment);   // my page id =123456789
    var_dump($status);
    }
    }
    ?>
    <body>
     <!-- Form for uploading the photo -->
     <div class="main">
      <p>Select a photo to upload on Facebook Fan Page</p>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
      <p>Select the image: <input type="file" name="pic" /></p>
      <p><input class="post_but" type="submit" value="Upload to my album" /></p>
      </form>
     </div>
    </body>

Link to comment
Share on other sites

action="<?php echo $_SERVER['PHP_SELF']; ?>"

There is no point in this. It's only a security risk, and as I said it's pointless, because you can do this instead:

action=""

 

if (isset($_FILES) && !empty($_FILES))

This is also pretty pointless in my opinion, because this does the same:

if(!empty($_FILES))

 

Something I scrambled down now... see if you can make any sense of it! I tested it to work, and it uploads and place the file in the same folder with a unique name. You can then do whatever you want with it from there.

<?php
if(!empty($_FILES)){
while(true){
	$unique_temp_filename = uniqid('TMP_PIX_', true).'.'.preg_replace('/.*?\//', '', $_FILES['pic']['type']);
	if(!file_exists(basename($unique_temp_filename))){
		break;
	}
}
move_uploaded_file($_FILES['pic']['tmp_name'], basename($unique_temp_filename));
echo realpath($unique_temp_filename);
}
?>
<form method="post" action="" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>

Link to comment
Share on other sites

:confused:

 

Thanx for providing me a piece of code ...

I have made successfully this functionality by uploading images first on my server and give its respective url to upload that pic on my FB FAN PAGE

 

I wanted to do something like without uploading it on my server and  directly upload it to my facebook page if you can give me a piece of code for that i'll really appreciate that..

 

Even i did that too successfully  but when i put that piece of code in this one that didn't work and started to give errors like unvalid file format etc as mentioned in the description....

 

 

OLD CODE THT SUCCESSFULY UPLOADS IMAGES TO MY CERTAIN ALBUM OF PAGE

if(isset($_FILES) && !empty($_FILES)){

if( !in_array($_FILES['pic']['type'], $valid_files ) ){

  echo 'Only jpg, png and gif image types are supported!';

}else{

  #Upload photo here

  $img = realpath($_FILES["pic"]["tmp_name"]);

 

  $args = array(

  'message' => 'This photo was uploaded via Radiations3',

  'image' => '@' . $img,

  'aid' => $album_id,

  'no_story' => 1,

  'access_token' => $fanpage_token

  );

 

  $photo = $facebook->api('/194458563914948/feed', 'post', $args);

  if( is_array( $photo ) && !empty( $photo['id'] ) ){

  echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';

  }

Link to comment
Share on other sites

Please anyone correct me if I'm wrong!

 

I believe your server needs to receive the image for it to work with PHP. Maybe JavaScript can do it without uploading to your server?

Whenever you use a form and that $_FILES global, I believe you upload the image to the server.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.