Script PHP download File dengan CURL

Bismillaahirrohmaanirrohiim…

Kali ini kita akan membuat script PHP CURL untuk mendownload file dengan format apa saja.

Fungsi download Gambar dibawah ini mempunyai keterangan:

– Bisa mendownload file via URL (http atau https)
– Lokasi folder bisa ditentukan
– File yang disupport txt, pdf, zip, mp3, mp4, doc, pdf, xls, dll
– Terdapat validasi pembuatan folder
– Terdapat validasi URL
– Terdapat validasi berhasil atau tidaknya gambar disimpan
CURL bisa diset timeoutnya
– Jika terjadi error, CURL bisa diketahui jenis kesalahannya
– Output response berupa array yang mudah dipakai

Langsung saja, berikut fungsi PHP download file dengan CURL

<?php
//Script PHP download File dengan CURL
function downloadFile($file_url, $dir_location='') {
    $returns = array();

    if (!empty($dir_location) AND !is_dir($dir_location)) {
        if(!mkdir($dir_location, 0777, true)) {
            $returns['status'] = 'error';
            $returns['message'] = 'gagal membuat folder '.$dir_location;
            return $returns;
        }
    }
    
    $url = filter_var($file_url, FILTER_SANITIZE_URL);    
    if (filter_var($url, FILTER_VALIDATE_URL) === false) {
        $returns['status'] = 'error';
        $returns['message'] = 'URL tidak valid';
        return $returns;
    }

    $path = parse_url($file_url, PHP_URL_PATH);
    $file_name = basename($path);//pathinfo($file_url, PATHINFO_FILENAME);//filename
    $file_ext = pathinfo($file_url, PATHINFO_EXTENSION);//ext
    $file_ext = strtolower($file_ext);
    
    if (empty($file_name)) {
        $returns['status'] = 'error';
        $returns['message'] = 'Nama file tidak valid';
        return $returns;
    }
    
    if (strpos($file_ext, '?')!==false) {
        $file_ext = substr($file_ext,0,strpos($file_ext, '?'));
    }
    
    $ch = curl_init ();
    curl_setopt($ch, CURLOPT_URL,$file_url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36');
    //curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    //curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    $raw = curl_exec($ch);

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_error = curl_error($ch);
    curl_close ($ch);
    
    if (!empty($curl_error) OR $http_status!=200) {
        $returns['status'] = 'error';
        $returns['message'] = 'http status: '.$http_status.' '.$curl_error.$file_url;
        return $returns;
    }



    $saveto = $dir_location.$file_name;
    if (file_exists($saveto)) {
        @unlink($saveto);
    }
    
    @file_put_contents($saveto, $raw);

    
    if (!is_file($saveto)) {
        $returns['status'] = 'error';
        $returns['message'] = 'Gagal simpan gambar';
        return $returns;
    }
    
    $returns['status'] = 'ok';
    $returns['message'] = 'success';
    $returns['file_url'] = $file_url;
    $returns['dir_location'] = $dir_location;    
    $returns['file_name'] = $file_name;
    $returns['saveto'] = $saveto;
    
    return $returns;
}

$dir_location = 'filez/';


$file_url = 'https://fossbytes.com/wp-content/uploads/2016/02/learn-to-code-what-is-programming.jpg';
$file_url = 'https://download.quranicaudio.com/quran/ahmed_ibn_3ali_al-3ajamy/078.mp3';

if (isset($_GET['url'])) {
    $file_url = $_GET['url'];
}

$downloadFile = downloadFile($file_url, $dir_location);

if (isset($downloadFile['saveto'])) {
    echo '<h1>FILE</h1>';
    echo '<a href="'.$downloadFile['saveto'].'">'.$downloadFile['saveto'].'</a>';
}

echo '<pre>';
print_r($downloadFile);
echo '</pre>';

 

Baca Juga:   AMP FIX Cors Origin dengan PHP

Fungsi PHP ini statusnya tinggal pakai, tidak ribet.

Kesimpulan

Oke, sementara begitu dulu tutorial cara membuat script download file dengan PHP CURL.

Jika ada error atau ada yang ingin ditanyakan silahkan tinggalkan komentar dibawah ini.

Semoga tutorial ini bermanfaat.

Bagikan

You May Also Like

About the Author: rasupe

Leave a Reply

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