【问题标题】:How-To Upload Photos to facebook(graph api) with html file selector(input type="file")如何使用 html 文件选择器(输入类型 =“文件”)将照片上传到 facebook(图形 api)
【发布时间】:2011-10-07 13:48:53
【问题描述】:

我有一个 html 文件,可以从用户的计算机中选择图像。下面给出了代码

  <html>
    <body>
    <form enctype="multipart/form-data" action="http://localhost/uploader/upload.php" method="POST">
    Please choose a photo: 
    <input name="source" type="file"><br/><br/>
    Say something about this photo: 
    <input name="message" type="text" value=""><br/><br/>
    <input type="submit" value="Upload"/><br/>
    </form>
    </body>  
 </html>

当我按下上传按钮时,我需要将所选图像的真实路径传递到upload.php文件中。upload.php的代码如下所示

<?php
    include_once 'fbmain.php';
    //some codes 
    try{
    $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        $token = $session['access_token'];//here I get the token from the $session array
        $album_id = '2179901265385';//MY ALBUM ID

        //upload my photo
        $FILE_PATH= 'HERE_I_NEED_THE_REAL_PATH_OF_THE_IMAGE';
        $facebook->setFileUploadSupport(true);
        $args = array('message' => 'Photo Caption');
        $args['image'] = '@' . realpath($FILE_PATH);

        $data = $facebook->api('/'. $album_id . '/photos?access_token='. $token, 'post', $args);

    } catch(FacebookApiException $e){
        echo "Error:" .$e;
    }    
?>

当我给出变量$FILE_PATH (eg: $FILE_PATH = 'C:\My Documents\My Pictures\a.jpg') 的路径时,它工作正常。但我需要从 html 文件选择器中获取此路径。
有没有办法做到这一点?
至少有人能告诉我一种访问文件选择器文本字段值的方法吗?($_POST['texboxname'] 在这里不起作用)。
I could find many tutorials which upload images into facebook using graph api but nothing with html file selector
那么有人可以帮助我吗?

【问题讨论】:

  • 查找php.net/manual/en/function.move-uploaded-file.php然后将照片保存在临时目录中并在上传后删除
  • 首先非常感谢您的回复。您能否详细解释一下。如何将照片保存在临时目录中并在上传后删除。移动到临时目录后如何获取真实路径。请解释一下。谢谢

标签: php html facebook facebook-graph-api html-input


【解决方案1】:

试试这个:只是一个例子

<html>
    <body>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Please choose a photo: 
    <input name="photo" type="file"><br/><br/>
    Say something about this photo: 
    <input name="message" type="text" value=""><br/><br/>
    <input type="submit" value="Upload"/><br/>
    </form>
    </body>  
 </html>


<?php 
//upload.php
if(isset($_FILES['photo']) && isset($_POST['message'])){

    $uploadfile = './uploads/'.basename($_FILES['photo']['name']);

    $iStats=getimagesize($_FILES['photo']['tmp_name']);

    if (isset($iStats['mime']) && $iStats[0]>0) {
        move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
        include_once 'fbmain.php';
        try{
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');
            $token = $session['access_token'];//here I get the token from the $session array
            $album_id = '2179901265385';//MY ALBUM ID
            $facebook->setFileUploadSupport(true);
            $args = array('message' => $_POST['message']);
            $args['image'] = '@' . realpath($uploadfile);

            $data = $facebook->api('/'. $album_id . '/photos?access_token='. $token, 'post', $args);

        } catch(FacebookApiException $e){
            echo "Error:" .$e;
        }
        unlink($uploadfile);
        echo "Success!\n";
    } else {
        echo "Wrong file type!\n";
    }
}

?>

【讨论】:

  • 你是个天才。它工作得很好。我只需要在我的工作目录中创建一个名为“uploads”的文件夹。非常感谢朋友..!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 2012-03-21
  • 1970-01-01
相关资源
最近更新 更多