views:

16

answers:

1

I wrote a Facebook app in PHP and need to be able to allow the user to upload an image to my server. I have used this code:

<?php
include_once('facebook.php');
$appapikey = 'API KEY HERE';
$appsecret = 'SECRET KEY HERE';
$facebook = new Facebook($appapikey, $appsecret);
$fb_user =  $facebook->require_login();

if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000)) {
      $newname = dirname(__FILE__).'/upload/zbt_'.$fb_user.'.jpg';
      if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
         header("Location: http://majik.zbrowntechnology.info/display.php");
      } else {
         header("Location: home.php?Fatal");
      }
 } else {
     header("Location: home.php?Fatal");
  }
} else {
 header("Location: home.php?Fatal");
}
?>

but am not able to actually save the file in the directory. I have done some playing around with the code and think the problem lies in the moving of the file in this line:

if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {....

A: 

It is not easy to do with FBML application.

First - you have to point your form to the script on your domain

<form enctype="multipart/form-data"
       action="http://your.domain/upload.php"
       method="post" >

Next: on that upload.php you have to handle the uploaded file, process it, do whatever you want and after that you need to redirect back to the your FBML canvas application at facebook.com.

zerkms
This is how the application is setup: User loads the FB canvas page. On the FB canvas page, there is an HTML iframe. This iframe contains the actual functionality of the app. (The upload form) The user chooses a file, then clicks the upload button. The form POSTs the form data to the upload script. The script then redirects to the display page. All the redirection is done inside the iframe.
Zachary Brown
This workflow seems to be correct. Well, debug your script then
zerkms
I figured it out. The issue was the code I used to get the current user's ID from Facebook. Fixed it by not including that code, but adding the user's ID at the end of each URL. I thank all of you for your help, and am going to add oyur names and profile images to the credits page! Thanks!
Zachary Brown