How to Upload a Remote File to Vikingfile Using PHP (Simple URL Upload Script)

In this tutorial, we will create a simple PHP script to upload a remote file URL to Vikingfile automatically. This method is commonly used in file sharing websites and automation systems where files are mirrored from another server.

The script performs these steps:

  • Accepts a remote file URL
  • Requests the Vikingfile upload server
  • Sends the file link to Vikingfile
  • Receives the final uploaded file URL
  • Returns JSON output

Complete PHP Code


<?php

/*
|--------------------------------------------------------------------------
| VIKINGFILE HELPERS
|--------------------------------------------------------------------------
*/

function json_out($arr)
{
    echo json_encode($arr, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

function curl_request($url, $postFields = null, $stream = false, &$finalJson = null)
{
    $buffer = '';

    $ch = curl_init($url);

    $options = [
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_USERAGENT => 'Mozilla/5.0 PHP/' . PHP_VERSION,
        CURLOPT_HTTPHEADER => [
            'Accept: application/json, text/plain, */*'
        ],
    ];

    if ($postFields === null) {
        $options[CURLOPT_RETURNTRANSFER] = true;
    } else {
        $options[CURLOPT_POST] = true;
        $options[CURLOPT_POSTFIELDS] =
            is_array($postFields)
            ? http_build_query($postFields)
            : $postFields;

        $options[CURLOPT_HTTPHEADER][] =
            'Content-Type: application/x-www-form-urlencoded';
    }

    if ($stream) {
        $options[CURLOPT_WRITEFUNCTION] =
        function ($ch, $chunk)
        use (&$buffer, &$finalJson) {

            $buffer .= $chunk;

            while (($pos = strpos($buffer, "\n")) !== false) {

                $line = trim(substr($buffer, 0, $pos));

                $buffer = substr($buffer, $pos + 1);

                if ($line === '') {
                    continue;
                }

                $json = json_decode($line, true);

                if (is_array($json)
                && isset($json['name'], $json['hash'], $json['url'])) {

                    $finalJson = $json;
                }
            }

            return strlen($chunk);
        };
    }

    curl_setopt_array($ch, $options);

    $response = curl_exec($ch);

    $error    = curl_error($ch);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    return [
        'body'      => $response,
        'error'     => $error,
        'http_code' => $httpCode
    ];
}

/*
|--------------------------------------------------------------------------
| CONFIG
|--------------------------------------------------------------------------
*/

$vikingUserHash = 'YOUR_VIKING_USER_HASH';

/*
|--------------------------------------------------------------------------
| INPUT
|--------------------------------------------------------------------------
*/

$linkUrl = 'https://example.com/file.zip';

if (empty($linkUrl)) {

    json_out([
        'status' => 'error',
        'message' => 'linkUrl is required'
    ]);
}

/*
|--------------------------------------------------------------------------
| GET VIKING UPLOAD SERVER
|--------------------------------------------------------------------------
*/

$serverRes =
curl_request(
    'https://vikingfile.com/api/get-server'
);

$serverJson =
json_decode(
    $serverRes['body'],
    true
);

$uploadServer =
rtrim(
    $serverJson['server'],
    '/'
);

/*
|--------------------------------------------------------------------------
| UPLOAD TO VIKINGFILE
|--------------------------------------------------------------------------
*/

$finalUploadJson = null;

$uploadRes =
curl_request(
    $uploadServer,
    [
        'link' => $linkUrl,
        'user' => $vikingUserHash
    ],
    true,
    $finalUploadJson
);

$vikingFileUrl =
trim(
    $finalUploadJson['url']
);

/*
|--------------------------------------------------------------------------
| FINAL OUTPUT
|--------------------------------------------------------------------------
*/

json_out([
    'status' => 'success',
    'file_link' => $vikingFileUrl
]);

?>

How This Script Works

1. Set Remote File URL

$linkUrl = 'https://example.com/file.zip';

This is the original file link that Vikingfile will download and store on its server.


2. Get Viking Upload Server

https://vikingfile.com/api/get-server

Vikingfile uses dynamic upload servers. The script first requests the correct server before uploading the file.


3. Upload Remote URL


'link' => $linkUrl,
'user' => $vikingUserHash

The script sends:

  • The remote file URL
  • Your Viking user hash

Vikingfile then downloads the file directly from your URL.


4. Receive Final File URL

$vikingFileUrl = $finalUploadJson['url'];

After upload completes, Vikingfile returns the final file link.


Example Success Response


{
  "status": "success",
  "file_link": "https://vikingfile.com/f/abc123"
}

Requirements

  • PHP with cURL enabled
  • Valid Viking user hash
  • Public remote file URL

Use Cases

  • File sharing website mirror upload
  • Remote URL upload automation
  • Backup file system
  • Multi-host uploader
  • Video hosting mirror

Common Errors

linkUrl is required


{
  "status": "error",
  "message": "linkUrl is required"
}

This means the file URL was empty.


Final Viking file URL not found

This usually means:

  • File URL is invalid
  • Server blocked the request
  • Upload failed

Conclusion

In this tutorial, we created a simple PHP script that uploads a remote file to Vikingfile using a direct URL. The script automatically finds the correct upload server and returns the final Vikingfile link. This method is reliable for file sharing websites and automation systems.

Leave a Comment

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

Scroll to Top