I'm trying to make an image upload form for twitter to change avatar. Twitter says the image has to be base 64 encoded before I upload it. So here's a sample
https://dev.twitter.com/docs/api/1/post/account/update_profile_image
How can I do this so far I've been getting errors
PHP Code:
<?php // process form data
if ($_POST['image']){
// post profile update
$post_data = array(
"image" => $_POST['image']
);
$post_data = base64_encode($post_data);
echo $post_data;
}
else{
echo
" <form action='post.php' method='POST' enctype='multipart/form-data'>
<br><input type='hidden' name='image' value='update_profile_image'>
<input type='file' name='image' size='30'/><br>
<input type='submit' value='开发者_C百科Upload' class='button'/>
</form>";}
?>
Please help
From the looks of this, you aren't retrieving the image from the POST at all. The image itself will be available in $_FILES
// See what's in $_FILES
var_dump($_FILES);
// You need the temporary name of your image from $_FILES
$filedata = file_get_contents($_FILES['image']['tmp_name']);
$post_data = base64_encode($filedata);
// Now you should have a base64 ascii string
echo $post_data;
This should do it. Move the uploaded file to a directory because you cannot get required results by encrypting the [tmp_name]
if ($_FILES['image']){
move_uploaded_file($_FILES["image"]["tmp_name"], "path" .basename($_FILES["image"]["name"]));
$meh = file_get_contents("path/" .basename($_FILES["image"]["name"]));
$results = base64_encode($meh);
}
精彩评论