简体   繁体   中英

PHP POST a file via AnonFiles with html form

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM