PHP kullanarak bir dosya yüklemeyi hizmet

4 Cevap php

First PHP project and I'm stuck!
I wish to allow users to click a button or a link and download a file.

Ancak, benim PHP ilk, bazı görevleri yerine doğru dosyayı seçin, Bu Yapabileceğim bir db vb indirme olayı kaydetmek, ama nasıl sonra kullanıcının tıklama bir tepki olarak dosya göndermek mi gerekir?

Herhangi bir yardım için teşekkür ederiz.

4 Cevap

@ Sarfraz anlaşılacağı gibi, sen, yapmanız gereken görevleri yaptıktan sonra, tarayıcıya bir Content-Type başlık göndermek ve sonra tarayıcıya dosyanın içeriğinin dışarı kusmak olabilir. Tarayıcı sonra genel bir) açık ya olacak ve dosyayı görüntüleyebilir veya b) diske dosyayı kaydetmek, hangi kullanıcı ayarlarına göre gerçekleştirecektir.

Eğer diskin yerine tarayıcıda görüntülenir kaydedilecek dosyayı zorlamak istiyorsanız, yaygın olarak kullanılan bir yöntem Content-Type: application/octet-stream bir mim-türünü belirtmek için. Bu başlığı ile, hem de bir ek dosya adı belirtmek mümkündür Content-Disposition: attachment; filename=foobar.baz.

Lütfen komut uygun başlık gönder:

header("content-type: application/pdf");

Dosyanızın başına doğru mime-type kullanın kullanarak tarayıcıya içerik göndermek readfile .

header("Content-type: image/png"); (ya da her neyse), ve dosya daha sonra sadece çıktı.

Burada doğrudan tarayıcıya dosya göndermek için kullanılan bir işlevdir.

$fileName: the path+name of the file that needs to be sent to the browser $downloadName: this is the name (no path needed) of the file that the user sees in it's download (is not necessarly the same as $filename

 function sendFileDirectlyToBrowser($downloadName, $fileName) {
        if (! file_exists($fileName)) {
            throw new Exception('file does not exist!');
        }

        $pathInfo = pathinfo($fileName);

        //list with mime-types http://en.wikipedia.org/wiki/Mime_type#List_of_common_media_types

        switch (strtolower($pathInfo['extension'])) {
            case 'csv':
                header("Content-type: test/csv");
                break;
            case 'doc':
            case 'docx':
                header("Content-type: application/msword");
                break;
            case 'gif':
                header("Content-type: image/gif");
                break;
            case 'jpg':
            case 'jpeg':
                header("Content-type: image/jpeg");
                break;
            case 'png':
                header("Content-type: image/png");
                break;
            case 'pdf':
                header("Content-type: application/pdf");
                break;
            case 'tiff':
                header("Content-type: image/tiff");
                break;
            case 'txt':
                header("Content-type: text/plain");
                break;
            case 'zip':
                header("Content-type: application/zip");
                break;
            case 'xls':
            case 'xlsx':
                if(!(strpos($HTTP_USER_AGENT,'MSIE') === false)) {
                    header("Content-type:application/x-msdownload");
                }
                else {
                    header("Content-type:application/vnd.ms-excel ");
                }
                break;
        }


        header('Content-Disposition:attachment;filename="'  . $downloadName . '"');
        print file_get_contents($fileName);
    }