PDA

View Full Version : giúp em về việc đọc file qua PHP (giấu directlink)



namduong8889
04-05-2009, 13:31
CHào các bác...

Em muốn giấu link của một file A.mp3 (đặt trên server A) qua một file download.php đặt trên server B thì phải làm thế nào? Để khi người dùng download file A.mp3 thì họ sẽ đánh link là http://www.serverB.com/Download.php chứ không nhìn thấy link trực tiếp của file A.mp3 (http://serverA.com/A.mp3)

Em đã thử code này, đã download được file a.mp3 về, nhưng file a.mp3 bị lỗi và không nghe được; Em paste link http://serverB.com/download.php vào Window media player để cho nó chạy cũng không nghe được)



<?php

dl_file("http://serverA.com/a.mp3");

function dl_file($file){


//Gather relevent info about file
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg"; break;
case "wav": $ctype="audio/x-wav"; break;
case "mpeg":
case "mpg":
case "mpe": $ctype="video/mpeg"; break;
case "mov": $ctype="video/quicktime"; break;
case "avi": $ctype="video/x-msvideo"; break;

//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

default: $ctype="application/force-download";
}

//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");

//Use the switch-generated Content-Type
header("Content-Type: $ctype");

//Force the download
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ");
@readfile($file);
exit;
}



[=========> Bổ sung bài viết <=========]

THÔI EM LÀM ĐƯỢC RỒI... phải đọc chính xác filesize qua hàm get_remote_file_size (google) mới down được...... hehe!

ngoc_viet08
04-05-2009, 19:20
tự biên tự diễn luôn . nhưng như thế cũng hay , anh em có cái học hỏi =))