dongyu5104 2018-09-15 15:22
浏览 62

PHP通过带有html格式的AnonFiles发布文件

I saw that AnonFiles allows a user to upload files directly to his account using their API: https://anonfiles.com/docs/api

I created an account and they gave me a API key, and with this key I can upload straight into my account by appending for example ?token=c9516efd61XXXXXX to the upload request.

I now want a simple form with PHP code that allows me to pick a file and upload it to my anonfiles account.

Here is my code, would you please advise what I am doing wrong, when I click Send I get no response back from anonfiles.

    if(isset($_POST['submit']))
        {
            $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
            $filename = $_FILES['file']['name'];

            $url = 'https://anonfiles.com/api/upload?token=c9516efd61XXXXXX;
            $myvars = 'myvar1=' . $attachment . '&myvar2=' . $filename;

            $ch = curl_init( $url );
            curl_setopt( $ch, CURLOPT_POST, 1);
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt( $ch, CURLOPT_HEADER, 0);
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

            $response = curl_exec( $ch );

            echo $response;
        }



<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><br /><label for="file"><span class="fs20"><span class="ff1 cf0" style="color:black;">File:              </span></span></label> <input type="file" name="file" id="file"></p>
<p><br /><input type="submit" name="submit" id="submit" value="Send"></p>
</form>
  • 写回答

1条回答 默认 最新

  • dongshao4207 2018-09-20 16:47
    关注

    Anonfiles support helped in this case, here is the code:

    if (isset($_POST['submit'])) {
    
        $url = sprintf('https://anonfile.com/api/upload?token=%s', $token);
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, [
            'file' => curl_file_create(
                $_FILES['file']['tmp_name'],
                $_FILES['file']['type'],
                $_FILES['file']['name']
            ),
        ]);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $json = curl_exec($ch);
    
        curl_close($ch);
    
        $result = json_decode($json);
    
        if (is_object($result) && $result->status) {
            echo "<p>OK!</p>";
        } else {
            echo "<p>Error uploading file.</p>";
        }
    }
    
        echo '
        <form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">
            <p>
                <br/>
                <label for="file">
                    <span class="fs20">
                        <span class="ff1 cf0" style="color:black;">File:              </span>
                    </span>
                </label>
                <input type="file" name="file" id="file">
            </p>
            <p>
                <br/>
                <input type="submit" name="submit" id="submit" value="Send">
            </p>
        </form>
      ';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行