如何使用 html 文件选择器将照片上传到 facebook(graph api)(输入类型=“文件”)

发布于 2024-11-24 21:57:58 字数 1636 浏览 5 评论 0原文

我有一个 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 的路径(例如:$FILE_PATH = 'C:\My Documents\My Pictures\a.jpg') 它工作正常。但我需要从 html 文件选择器获取此路径.
有办法做到这一点吗?
至少有人可以告诉我一种访问文件选择器文本字段值的方法吗?($_POST['texboxname'] 在这里不起作用)。
我可以找到很多使用图形 API 将图像上传到 facebook 的教程,但没有使用 html 文件选择器
那么有人可以帮助我吗?

I have a html file which select images from user's computer.code is given bellow

  <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>

When I press upload button,I need to pass the real path of the selected image into upload.php file.code of the upload.php is given bellow

<?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;
    }    
?>

When I give a path to variable $FILE_PATH (eg: $FILE_PATH = 'C:\My Documents\My Pictures\a.jpg') it works fine.But I need to take this path from html file selector.
Is there a way to do this?
At least can anyone tell me a way to access the value of the text field of file selector?($_POST['texboxname'] doesn't work here).
I could find many tutorials which upload images into facebook using graph api but nothing with html file selector.
So can anyone please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-12-01 21:57:58

试试这个:只是一个例子

<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";
    }
}

?>

Try This: Only an example

<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";
    }
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文