Search
 
SCRIPT & CODE EXAMPLE
 

PHP

download file php

1.  <?php
2.  $dir="download/";
3.  $filename=$_GET['file'];
4.  $file_path=$dir.$filename;
5.  $ctype="application/octet-stream";
6.  //
7.  if(!empty($file_path) && file_exists($file_path)){ //check keberadaan file
8.    header("Pragma:public");
9.    header("Expired:0");
10.   header("Cache-Control:must-revalidate");
11.   header("Content-Control:public");
12.   header("Content-Description: File Transfer");
13.   header("Content-Type: $ctype");
14.   header("Content-Disposition:attachment; filename="".basename($file_path).""");
15.   header("Content-Transfer-Encoding:binary");
16.   header("Content-Length:".filesize($file_path));
17.   flush();
18.   readfile($file_path);
19.   exit();
20. }else{
21.   echo "The File does not exist.";
22. }
23. ?>
Comment

file downloading in php

<?php
if(isset($_REQUEST["file"])){
// Get parameters
    $file = urldecode($_REQUEST["file"]); // Decode URL-encodedstring
    $filepath = "images/" . $file;

// Process download
    if(file_exists($filepath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        flush(); // Flush system output buffer
        readfile($filepath);
        exit;
    }
}
?>
Comment

php file download from url

Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
From the manual:

If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream().

(Thanks Hakre.)
Comment

How to Download a File in PHP

<?php
if(isset($_REQUEST["file"])){
    // Get parameters
    $file = urldecode($_REQUEST["file"]); // Decode URL-encoded string

    /* Check if the file name includes illegal characters
    like "../" using the regular expression */
    if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
        $filepath = "images/" . $file;

        // Process download
        if(file_exists($filepath)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));
            flush(); // Flush system output buffer
            readfile($filepath);
            die();
        } else {
            http_response_code(404);
	        die();
        }
    } else {
        die("Invalid file name!");
    }
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: get ip address in laravel 
Php :: dompdf with qr code 
Php :: bigtext migration laravel 
Php :: add two numbers as string in php 
Php :: base url laravel 
Php :: valet switch php version 
Php :: Laravel 5.4 Route back in blade 
Php :: php return a json response 
Php :: Carbon fomart date 
Php :: php artisan ui tailwind css 
Php :: php array remove key value pair 
Php :: how to cheeck php 
Php :: supervisor configuration for laravel queue 
Php :: wordpress get post type 
Php :: current user wordpress 
Php :: set laravel local time to indonesia 
Php :: create livewire component 
Php :: Clear from faild_jobs laravel 
Php :: laravel migrate specific table 
Php :: laravel print to log 
Php :: php destroy session after some time 
Php :: How to convert a PHP array to JSON object 
Php :: use id as key in co;lection laravel 
Php :: laravel created_at changing 
Php :: display error php 
Php :: laravel eloquent mass update 
Php :: textarea laravel migration 
Php :: wpdb get results foreach 
Php :: str_ireplace 
Php :: laravel pagination keep query string 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =