Board logo

標題: [技術討論] 用php readfile 去download mp4 [打印本頁]

作者: as123123    時間: 2016-5-3 17:10     標題: 用php readfile 去download mp4

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=text.mp4");
header("Content-Type: video/mp4");
header("Content-Transfer-Encoding: binary");
readfile("http://www.aaa/text.mp4");

點解download完播唔到, 話格式不支援.

我直接用另存檔案又可以播.

download完file size都一樣.
作者: kin021360    時間: 2016-5-3 17:23

(就咁check file size唔夠準, 最好用checksum)
作者: twaiho2003    時間: 2016-5-3 17:28

本帖最後由 twaiho2003 於 2016-5-3 17:30 編輯

試下用node.js, 你copy我段code改左host同path個value , 跟住直接run

var fs = require('fs')
var http = require('http')

var options = {host:"www.aaa", path:"/text.mp4"}

write_to_file(options, "test.mp4")

function write_to_file(options, filename) {

        http.get(options, function(res){
                        var imagedata = ''
                        res.setEncoding('binary')

                        res.on('data', function(chunk){
                                        imagedata += chunk
                        })

                        res.on('end', function(){
                                        fs.writeFile( filename, imagedata, 'binary', function(err){
                                                        if (err) throw err
                                        })
                        })

        })

}
作者: stta800    時間: 2016-5-3 17:48

試下用node.js, 你copy我段code改左host同path個value , 跟住直接run

var fs = require('fs')
var http =  ...
twaiho2003 發表於 2016-5-3 17:28



    node.js 直頭用人地寫好library啦
作者: twaiho2003    時間: 2016-5-3 17:53

回覆 4# stta800


    有d難度果D就會用, 例如download youtube
作者: as123123    時間: 2016-5-3 19:25

回覆 3# twaiho2003

run左無反應
作者: twaiho2003    時間: 2016-5-3 20:31

回覆 6# as123123


    冇反應係正常,  有個test.mp4會download左係你run果個directory
作者: as123123    時間: 2016-5-3 20:44

node.js 我完全唔識,
有無得用返php 可以 download 到?
作者: stta800    時間: 2016-5-3 20:53

php用composer搵library
作者: twaiho2003    時間: 2016-5-3 21:06

回覆 8# as123123

改左個$url同$img value,  再改大php memory limit, 因為file_get_contents好食ram, download片可能會唔夠
    <?php

$url = 'https://upload.wikimedia.org/wikipedia/commons/4/49/Dell_Inspiron_One_23_Touch_AIO_Desktop_PC.png';
$img = 'flower.png';
file_put_contents($img, file_get_contents($url));

?>
作者: 梁炳    時間: 2016-5-3 21:25

咁做法....個mp4有幾大,你個php就要幾多ram
無可能夠ram的

無論乜language都好,要用stream, 固定一個buffer size讀寫
好似php都有但我未用過
作者: twaiho2003    時間: 2016-5-3 23:00

本帖最後由 twaiho2003 於 2016-5-3 23:29 編輯

都係用php call wget download簡單D, 同樣改$url同$filename

<?php
$url = 'https://upload.wikimedia.org/wikipedia/commons/4/49/Dell_Inspiron_One_23_Touch_AIO_Desktop_PC.png';

$filename = "test.png";

exec("wget  " . $url );

?>
作者: KinChungE    時間: 2016-5-4 11:51

都係用php call wget download簡單D, 同樣改$url同$filename
twaiho2003 發表於 2016-5-3 23:00


server冇裝wget就死
作者: stta800    時間: 2016-5-4 13:49

server冇裝wget就死
KinChungE 發表於 2016-5-4 11:51



    係如果係咁寫不如直接打command
作者: KinChungE    時間: 2016-5-4 13:49

係如果係咁寫不如直接打command
stta800 發表於 2016-5-4 13:49


唔得, 有時web service又唔可以叫end user幫你打command
不過真係想咁做的話, 寫cgi比php更好
其實exec咁好危險
$url = "&& rm -rf /";

作者: stta800    時間: 2016-5-4 13:56

唔得, 有時web service又唔可以叫end user幫你打command
不過真係想咁做的話, 寫cgi比php更好
其實exec咁 ...
KinChungE 發表於 2016-5-4 13:49



    樓主淨係save落自己server, 直接係server打command咪得
唔通自己玩死自己
作者: supergag    時間: 2016-5-5 11:59

本帖最後由 supergag 於 2016-5-5 12:34 編輯
都係用php call wget download簡單D, 同樣改$url同$filename
twaiho2003 發表於 2016-5-3 23:00


exec無disable就行到...但唔安全

呢個應該work,不過file size越大就越食ram...好快玩死server
  1. <?php
  2. $file = 'http://www.test.com/video.mp4'; // file path
  3. header('Content-Description: File Transfer');
  4. header('Content-Type: application/octet-stream');
  5. header('Content-Disposition: attachment; filename="'.basename($file).'"');
  6. header('Expires: 0');
  7. header('Cache-Control: must-revalidate');
  8. header('Pragma: public');
  9. readfile($file);
  10. exit;
  11. ?>
複製代碼





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2