How to Upload a File to PixelDrain Using PHP cURL

In this tutorial, I will show how to upload a file to PixelDrain using PHP cURL. This method is useful if you are building a file sharing website, remote upload tool, or automation system.

The script will:

  • Send a local file to PixelDrain
  • Use API key authentication
  • Check for cURL errors
  • Check HTTP response errors
  • Read the uploaded PixelDrain file ID
  • Create the final PixelDrain URL

Complete PHP Code


<?php

$upload_url = 'https://pixeldrain.com/api/file/';
$auth_header = 'Basic ' . base64_encode(':' . $api_key);

$ch = curl_init();

$data = [
    'file' => new CURLFile($file_path),
    'name' => $name
];

curl_setopt_array($ch, [
    CURLOPT_URL => $upload_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => [
        "Authorization: $auth_header",
    ],
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    $error = curl_error($ch);
    curl_close($ch);

    json_out([
        'status' => 'error',
        'message' => 'Pixeldrain upload cURL error',
        'id' => $id,
        'curl_error' => $error
    ], 202);
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$json = json_decode($response, true);

if ($httpCode >= 400) {
    json_out([
        'status' => 'error',
        'message' => 'Pixeldrain upload failed',
        'id' => $id,
        'http_code' => $httpCode,
        'response' => $json ?: $response
    ], 202);
}

if (!is_array($json) || !isset($json['id'])) {
    json_out([
        'status' => 'error',
        'message' => 'Failed to upload file to PixelDrain',
        'id' => $id,
        'response' => $response
    ], 202);
}

$pixelid = $json['id'];
$pixeldrain_url = "https://pixeldrain.net/u/{$pixelid}";

Simple Full Working Example

If you want a more complete example with variables and JSON output, use this version:


<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);

$api_key   = 'YOUR_PIXELDRAIN_API_KEY';
$file_path = __DIR__ . '/test.zip';
$name      = 'test.zip';
$id        = '12345';

function json_out($data, $code = 200) {
    http_response_code($code);
    header('Content-Type: application/json');
    echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    exit;
}

if (!file_exists($file_path)) {
    json_out([
        'status' => 'error',
        'message' => 'File not found',
        'file_path' => $file_path
    ], 404);
}

$upload_url = 'https://pixeldrain.com/api/file/';
$auth_header = 'Basic ' . base64_encode(':' . $api_key);

$ch = curl_init();

$data = [
    'file' => new CURLFile($file_path),
    'name' => $name
];

curl_setopt_array($ch, [
    CURLOPT_URL => $upload_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => [
        "Authorization: $auth_header",
    ],
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    $error = curl_error($ch);
    curl_close($ch);

    json_out([
        'status' => 'error',
        'message' => 'Pixeldrain upload cURL error',
        'id' => $id,
        'curl_error' => $error
    ], 202);
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$json = json_decode($response, true);

if ($httpCode >= 400) {
    json_out([
        'status' => 'error',
        'message' => 'Pixeldrain upload failed',
        'id' => $id,
        'http_code' => $httpCode,
        'response' => $json ?: $response
    ], 202);
}

if (!is_array($json) || !isset($json['id'])) {
    json_out([
        'status' => 'error',
        'message' => 'Failed to upload file to PixelDrain',
        'id' => $id,
        'response' => $response
    ], 202);
}

$pixelid = $json['id'];
$pixeldrain_url = "https://pixeldrain.net/u/{$pixelid}";

json_out([
    'status' => 'success',
    'message' => 'File uploaded successfully',
    'id' => $id,
    'pixel_id' => $pixelid,
    'pixeldrain_url' => $pixeldrain_url
]);

How This Code Works

1. Set PixelDrain API URL

$upload_url = 'https://pixeldrain.com/api/file/';

This is the PixelDrain upload API endpoint where the file will be sent.

2. Create Authorization Header

$auth_header = 'Basic ' . base64_encode(':' . $api_key);

PixelDrain uses Basic Authorization with your API key. The username part is empty, and the API key is used as the password.

3. Prepare File Data

$data = [
    'file' => new CURLFile($file_path),
    'name' => $name
];

This sends the local file and also sets the display name for the upload.

4. Upload the File with cURL

CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data

This sends the file as a POST request to PixelDrain.

5. Check cURL Errors

if (curl_errno($ch))

If cURL fails because of timeout, SSL issue, or connection problem, the code returns a JSON error response.

6. Check HTTP Errors

if ($httpCode >= 400)

If the server returns an HTTP error like 400, 401, or 500, the script returns that error response.

7. Read Uploaded File ID

$pixelid = $json['id'];

After successful upload, PixelDrain returns a file ID. Using that ID, we can build the final public file URL.

8. Build Final PixelDrain Link

$pixeldrain_url = "https://pixeldrain.net/u/{$pixelid}";

This creates the final direct PixelDrain file page URL.


Example Success Response

{
  "status": "success",
  "message": "File uploaded successfully",
  "id": "12345",
  "pixel_id": "abcdEF12",
  "pixeldrain_url": "https://pixeldrain.net/u/abcdEF12"
}

Example Error Response

{
  "status": "error",
  "message": "Pixeldrain upload failed",
  "id": "12345",
  "http_code": 401,
  "response": "Unauthorized"
}

Requirements

  • PHP with cURL enabled
  • A valid PixelDrain API key
  • A local file path that exists on the server

Use Cases

  • File sharing websites
  • Remote upload tools
  • Automation scripts
  • Backup upload systems
  • Mirror upload systems

Important Notes

  • Make sure the file path is correct
  • Do not expose your API key publicly
  • Large files may take time depending on server speed
  • It is better to keep SSL verification enabled in production if possible

Conclusion

In this tutorial, we learned how to upload a file to PixelDrain using PHP cURL. This method is simple and useful for any custom uploader, file sharing project, or automation workflow. After the upload succeeds, the script reads the returned PixelDrain ID and creates the final share URL.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top