Search code examples
phpunity-game-engineassetbundle

How to force download unity asset bundle using php?


I am downloading an asset bundle using the following code:

<?php
$file_url = "AssetBundle/bundle-numb";
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($file_url));
readfile($file_url);
>?

However, only some bytes get downloaded and when I open the file I get an error saying

filesize(): stat failed for ....

When I try the same code for downloading other files it works fine, but not with asset bundles.

Asset bundles are LZMA compressed by default and I think the asset bundles do not have any file extentions.

Downloading works fine when I use the following directly in the browser:

http:XXXXXXXXXX.com/AssetBundle/bundle-numb 

Solution

  • I think i found the solution using php

    <?php
    $fileName = basename('bundle-rainbow');
    $filePath = 'AssetBundles/'.$fileName;
    if(!empty($fileName) && file_exists($filePath)){
        // Define headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$fileName");
        header("Content-Type: application/zip");
        header("Content-Length:".filesize($filePath));
        header("Content-Transfer-Encoding: binary");
    
        // Read the file
        readfile($filePath);
        exit;
    }else{
        echo 'The file does not exist.';
        echo $fileName;
    }
    ?>  
        // Read the file
        readfile($filePath);
        exit;
    }else{
        echo 'The file does not exist.';
        echo $fileName;
    }
    ?>