Bismillaahirrohmaanirrohiim…
Kali ini kita akan menciptakan script PHP CURL untuk mend0wnl0ad file dengan format apa saja.
Fungsi d0wnl0ad Gambar dibawah ini memiliki keterangan:
– Bisa mend0wnl0ad file via URL (http atau https)
– Lokasi folder sanggup 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 sanggup diset timeoutnya
– Jika terjadi error, CURL sanggup diketahui jenis kesalahannya
– Output response berupa array yang gampang dipakai
Langsung saja, berikut fungsi PHP d0wnl0ad file dengan CURL
<?php
//Script PHP d0wnl0ad File dengan CURL
function d0wnl0adFile($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 menciptakan 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://d0wnl0ad.quranicaudio.com/quran/ahmed_ibn_3ali_al-3ajamy/078.mp3';
if (isset($_GET['url'])) {
$file_url = $_GET['url'];
}
$d0wnl0adFile = d0wnl0adFile($file_url, $dir_location);
if (isset($d0wnl0adFile['saveto'])) {
echo '<h1>FILE</h1>';
echo '<a href="'.$d0wnl0adFile['saveto'].'">'.$d0wnl0adFile['saveto'].'</a>';
}
echo '<pre>';
print_r($d0wnl0adFile);
echo '</pre>';
Fungsi PHP ini statusnya tinggal pakai, tidak ribet.
Kesimpulan
Oke, sementara begitu dulu tutorial cara menciptakan script d0wnl0ad file dengan PHP CURL.
Jika ada error atau ada yang ingin ditanyakan silahkan tinggalkan komentar dibawah ini.
Semoga tutorial ini bermanfaat.
Sumber https://rasupe.com
Mari berteman dengan saya
Follow my Instagram _yudha58
0 Response to "Script Php D0wnl0ad File Dengan Curl"
Posting Komentar