How-To Upload Photos to facebook(graph api) with html file selector(input type=“file”)

有些话、适合烂在心里 提交于 2020-01-11 06:39:16

问题


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?


回答1:


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

?>


来源:https://stackoverflow.com/questions/6731554/how-to-upload-photos-to-facebookgraph-api-with-html-file-selectorinput-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!