PDA

View Full Version : Tạo trang download bằng php?



langtusitinh225
26-06-2009, 00:47
Giờ em muốn làm 1 trang download bằng php mục đích chính là để đếm coi có bao nhiu lượt download mà ko biết làm thế nào, các bác ai rành php giúp em với.
Link để download có dạng như: http://abc.com/?id=123

snoob
26-06-2009, 11:10
Code:


<?php
if(isset($_GET['id'])){

$file = $_GET['id'];
//// Get file link from db
if(file_exists('downloadfolder/'.$file)){
header('Content-disposition: attachment; filename='.$file);
readfile('downloadfolder/'.$file);
////Tăng lượt download here
}
}
else{
exit('You don\'t have any download');
}
?>
http://www.mediafire.com/imgbnc.php/27596cd789302fe07a242b228cabfd114g.jpg
Xem thêm: http://webdesign.about.com/od/php/ht/force_download.htm

langtusitinh225
26-06-2009, 18:13
Nghe nói cách này chỉ cố thể làm với các file cùng server thôi phải không bạn?

snoob
26-06-2009, 18:48
Bậy nào, nếu không tin bạn có thể xem thử:


<?php
header('Content-disposition: attachment; filename=rapid.zip');
header('Content-type: application/zip');
readfile("http://rapidleech2.googlecode.com/files/Rapidleech_PlugMod_v40.zip");
?>
Nhân tiện sửa lại code của mình chút


<?php
if(isset($_GET['id'])){
$file = $_GET['id'];
$info = pathinfo($file);
//// Get file link from db
if(file_exists($file)){
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/'. $info['extenstion']);
readfile($file);
////Tang lu?t download here
}
}
else{
exit('You don\'t have any download');
}
?>

ngoc_viet08
27-06-2009, 12:58
mấy cái header làm gì thế bồ tèo snoob :D

khuongCOMPUTER
27-06-2009, 13:01
Đặng báo cho trình duyệt biết: "Ê, cái này là file download này, cấm hiển thị nội dung ra lung tung đấy. Gọi trình download mà đưa link vô" :D

langtusitinh225
28-06-2009, 08:25
Cho em hỏi về ưu và nhược điểm của 2 cách làm này:


$filename = "http://localhost/" . $_GET['id'];
$info = pathinfo($file);
$fp = fopen($filename, "rb");
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$_GET['id'] . '"');
fpassthru($fp);




$file = "http://langtusitinh225.googlepages.com/" . $_GET['id'];
$info = pathinfo($file);
header('Content-disposition: attachment; filename='.$_GET['id']);
header('Content-type: audio/mpeg3');
readfile($file);

dctamtn
23-12-2009, 12:37
Nhìn ko hiểu lắm.Code mình còn yếu lắm