I'm using AnonFiles website to upload files directly to my 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=5846e48082XXXXXX
to the upload request.
Request Example
curl -F "file=@test.txt" https://api.anonfile.com/upload
Now i want a simple form with PHP code that allows me to pick a file and upload it to my anonfiles account.
Here is my try to write this request in PHP
using cURL
function
<?PHP
if (isset($_POST['submit'])) {
$url = 'https://anonfile.com/api/upload?token=5846e48082XXXXXX';
$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 "OK!";
} else {
echo "Error";
}
}
?>
HTML FORM
<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" id="file">
<br/>
<input type="submit" name="submit" id="submit" value="Send">
</form>
But this seems not working and print out Error message rather than ok and no file is uploaded.
You can do it like this:
<?PHP
if (isset($_POST['submit'])) {
$url = 'https://anonfile.com/api/upload?token=5846e48082XXXXXX';
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
if ($filedata != '')
{
$headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
$postfields = array("filedata" => "@$filedata", "filename" => $filename);
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
$json = curl_exec($ch);
$result = json_decode($json);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
if ($info['http_code'] == 200) {
if (is_object($result) && $result->status) {
$msg = "OK!";
}
}
}
else
{
$msg = curl_error($ch);
}
curl_close($ch);
}
else
{
$msg = "Please select the file";
}
echo $msg;
}
?>