url download file – How to download a file from a URL?

url download file : Force a file to download into your system with the help of PHP. Learn How to use PHP cURL Library for Download image or file from URL.

url download file

Download a file from the URL in PHP. i specified the full HTTP URL of the file that we want to download. Using PHP’s file_get_contents function, i downloaded the file

Download a file from the URL in PHP

Example

<?php 
$url        =   '<a class="googleadsense" href="https://www.pakainfo.com/wp-content/uploads/2022/08/tamilrokers.jpg" rel="nofollow"><span>https</span><span>://</span><span>pakainfo</span><span>.</span><span>com</span><span>/</span><span>wp</span><span>-</span><span>content</span><span>/</span><span>uploads</span><span>/</span><span>2022</span><span>/</span><span>08</span><span>/</span><span>tamilrokers</span><span>.</span><span>jpg</span></a>'; 
$urlPdf     =   '<a class="googleadsense" href="http://www.infinityknow.edu/images/thamb/document.pdf" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>infinityknow</span><span>.</span><span>edu</span><span>/</span><span>images</span><span>/</span><span>thamb</span><span>/</span><span>document</span><span>.</span><span>pdf</span></a>';
$path_url  =   basename($url); 
 
//save the file by using base name 
if(file_put_contents( $path_url,file_get_contents($url))){
    echo "File downloaded successfully!"; 
}else{
    echo "File downloading failed!"; 
}
?>

<?php
function df($urlFile){
    $path_url  =   basename($urlFile);
    //store the file by using base name
    $fn         =   file_put_contents($path_url,file_get_contents($urlFile));
    header("Expires: 0");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header("Content-type: application/file");
    header('Content-length: '.filesize($path_url));
    header('Content-disposition: attachment; filename="'.basename($path_url).'"');
    readfile($path_url);
}
?>

The usage of the above function is given below.

Read Also:  how to resize image in php

<?php
$url        =   '<a class="googleadsense" href="https://www.pakainfo.com/wp-content/uploads/2022/08/tamilrokers.jpg" rel="nofollow"><span>https</span><span>://</span><span>pakainfo</span><span>.</span><span>com</span><span>/</span><span>wp</span><span>-</span><span>content</span><span>/</span><span>uploads</span><span>/</span><span>2022</span><span>/</span><span>08</span><span>/</span><span>tamilrokers</span><span>.</span><span>jpg</span></a>'; 
$urlPdf     =   '<a class="googleadsense" href="http://www.infinityknow.edu/images/thamb/document.pdf" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>infinityknow</span><span>.</span><span>edu</span><span>/</span><span>images</span><span>/</span><span>thamb</span><span>/</span><span>document</span><span>.</span><span>pdf</span></a>';
df($urlPdf);
?>

Using CURL

Example

<?php
function getFlData($url){
    $ch     =   curl_init($url);
    $dir            =   '../admin/';
    $fileName       =   basename($url);
    $saveFilePath   =   $dir . $fileName;
    $fp             =   fopen($saveFilePath, 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}
?>

Uses

$urlPdf     =   '<a class="googleadsense" href="http://www.infinityknow.edu/images/thamb/document.pdf" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>infinityknow</span><span>.</span><span>edu</span><span>/</span><span>images</span><span>/</span><span>thamb</span><span>/</span><span>document</span><span>.</span><span>pdf</span></a>';
getFlData($urlPdf);

Don’t Miss : PHP Download File From URL Curl

I hope you get an idea about url download file.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.