2013-02-12 13 views
9

Codeigniter belgelerini inceleyerek, indirme dosyalarını sunucudan zorlamak için aşağıdaki kodu kullanıyorum.Codeigniter Force dosyaları karşıdan yükle

function download($file_id){ 
     $file = $this->uploadmodel->getById($file_id); //getting all the file details 
                 //for $file_id (all details are stored in DB) 
     $data = file_get_contents($file->full_path); // Read the file's contents 
     $name = $file->file_name;; 

     force_download($name, $data); 
    } 

Kod, görüntülerin çalışma dosyasıdır, ancak PDF dosyaları söz konusu olduğunda, çalışmıyor. Tüm dosya uzantıları için test etmedim, ancak PDF için çalışmadığından, diğer çeşitli dosya türleri için çalışmayabilir. Herhangi bir çözüm?

+0

Nasıl çalışmıyor? indirmiyor mu? bir PDF okuyucuda açılmıyor mu? hatalar? –

+1

evet .. 'download/32' yi oluşturduğum bağlantıya kestiğim zaman, sadece bağlantıyı açar ve boş bir sayfa. , "download/$ file_id" dir. – prakashchhetri

+0

.zip, .jpg, .txt, .gif hepsi çalışıyor, fakat değil .pdf – prakashchhetri

cevap

19

Benzer sorunlar yaşadım, sorunun bazı mime öğelerinde ve tarayıcılara gönderilen başlıklar içinde olduğunu düşünüyorum. Ben burada http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter bulundu kodunu kullanarak sona erdirmek. Force_download yerine aşağıdaki işlevi kullanın. Şimdiye kadar benim için çalıştı.

function _push_file($path, $name) 
{ 
    // make sure it's a file before doing anything! 
    if(is_file($path)) 
    { 
    // required for IE 
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } 

    // get the file mime type using the file extension 
    $this->load->helper('file'); 

    $mime = get_mime_by_extension($path); 

    // Build the headers to push out the file properly. 
    header('Pragma: public');  // required 
    header('Expires: 0');   // no cache 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT'); 
    header('Cache-Control: private',false); 
    header('Content-Type: '.$mime); // Add the mime type from Code igniter. 
    header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name 
    header('Content-Transfer-Encoding: binary'); 
    header('Content-Length: '.filesize($path)); // provide file size 
    header('Connection: close'); 
    readfile($path); // push it out 
    exit(); 
} 

Yardım edin.

+0

Belki de bu, codeigniter'ın eski sürümleri ile ilgili bir sorun oldu, ama şimdi ben codeigniter 2.2 kullanıyorum, pdf sorunu yok, ve RJ Anoop'un cevabı faydalıydı. – Musa

8

Ayrıca .pdf için çalışıyor! plz dosyasının yolunu kontrol edin. Bu sanırım sorun olabilir. Bende de bu problem vardı. Ama dosya yolunu düzelttikten sonra mükemmel çalıştı ... Aşağıdaki kodu nasıl yazdım !!!

if($src == "xyyx") 
{ 
$pth = file_get_contents(base_url()."path/to/the/file.pdf"); 
$nme = "sample_file.pdf"; 
force_download($nme, $pth);  
} 

Bu yardımcı olabilir umarım! Teşekkür ve saygıları

+0

Başlangıçta çalışmadı. sadece boş bir sayfa gösteriliyor. ".pdf" uzantısını eklemeyi unuttum – aphoe