×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃThinkPHP6¾ÙÐдóÎļþÉÏ´«ºÍÏÂÔزÙ×÷£¿

Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÎļþÉÏ´«ºÍÏÂÔØÒѾ­³ÉΪÍøÕ¾¿ª·¢Àú³ÌÖв»¿É»òȱµÄ¹¦Ð§¡£ÔÚ´¦Àí´óÎļþÉÏ´«ºÍÏÂÔØʱ£¬³ÌÐòµÄЧÂʺÍÎȹÌÐÔ±äµÃÓÈΪÖ÷Òª¡£¶øthinkphp6ÊÇÒ»¿î¹¦Ð§Ç¿Ê¢µÄphp¿ò¼Ü£¬¿ÉÒÔ×ÊÖúÎÒÃÇÓÐÓõØʵÏÖ´óÎļþÉÏ´«ºÍÏÂÔع¦Ð§¡£

Ò»¡¢´óÎļþÉÏ´«

ÔÚʹÓÃThinkPHP6¾ÙÐдóÎļþÉÏ´«Ê±£¬ÐèҪ˼Á¿µ½ÒÔϼ¸¸ö·½Ã棺

ÔÚÇ°¶ËÐèҪʹÓ÷ÖƬÉÏ´«ÊÖÒÕ£¬¼´½«ÎļþÖ§½â³É¶à¸öСÎļþ¾ÙÐÐÉÏ´«¡£Õâ¿ÉÒÔÓÐÓÃ×èÖ¹ÉÏ´«´óÎļþʱ·ºÆðµÄÍøÂçÖÐÖ¹µÈÎÊÌâ¡£

ÔÚºó¶ËÐèҪʹÓ÷ÖƬºÏ²¢ÊÖÒÕ£¬¼´½«·ÖƬÉÏ´«µÄСÎļþºÏ²¢³ÉÒ»¸öÍêÕûµÄ´óÎļþ¡£ÕâÐèҪʹÓÃPHPµÄÎļþ²Ù×÷º¯ÊýÀ´ÊµÏÖ¡£

ÏÂÃæÊÇÒ»¸ö¼òÆӵĴóÎļþÉÏ´«´úÂëʾÀý£º

//ÔÚ¿ØÖÆÆ÷Öнç˵´óÎļþÉÏ´«µÄÒªÁì

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

public function upload()

{

$chunk = input('param.chunk'); // »ñÈ¡Ä¿½ñÉÏ´«µÄ·ÖƬÐòºÅ
$chunks = input('param.chunks'); // »ñÈ¡·ÖƬ×ÜÊý

$file = request()->file('file'); // »ñÈ¡ÉÏ´«µÄÎļþ

$md5 = md5_file($file->getRealPath()); // »ñÈ¡ÉÏ´«ÎļþµÄMD5Öµ

$fileName = $md5 . '_' . $chunk . '.part'; // Æ´½Ó·ÖƬÎļþÃû

$file->move('./uploads/', $fileName); // ÉúÑÄ·ÖƬÎļþµ½Ð§ÀÍÆ÷

if ($chunk === $chunks - 1) { // ÈôÊÇÊÇ×îºóÒ»¸ö·ÖƬÎļþ
    $filePath = './uploads/' . $md5 . '_' . time() . '.mp4'; // Æ´½Ó×îÖÕÎļþÃû
    $fp = fopen($filePath, 'ab'); // ·­¿ª×îÖÕÎļþ
    for ($i = 0; $i < $chunks; $i++) { // Ñ­»·¶ÁÈ¡·ÖƬÎļþ²¢Ð´Èëµ½×îÖÕÎļþ
        $partFileName = './uploads/' . $md5 . '_' . $i . '.part'; // »ñÈ¡·ÖƬÎļþÃû
        $partFile = fopen($partFileName, 'rb');
        fwrite($fp, fread($partFile, filesize($partFileName))); // дÈëµ½×îÖÕÎļþ
        fclose($partFile); // ¹Ø±Õ·ÖƬÎļþ
        unlink($partFileName); // ɾ³ý·ÖƬÎļþ
    }
    fclose($fp); // ¹Ø±Õ×îÖÕÎļþ
    return 'ÉÏ´«Íê³É';
} else {
    return 'ÉÏ´«ÖÐ';
}

µÇ¼ºó¸´ÖÆ

}

ÔÚÇ°¶ËÒ³Ã棬¿ÉÒÔʹÓÃJavaScriptʵÏÖ·ÖƬÉÏ´«£º

function uploadFile(file) {

const size = file.size;
const chunkSize = 1024 * 1024; // ½«ÎļþÖ§½â³É1M¾ÞϸµÄ·ÖƬ
const chunkCount = Math.ceil(size / chunkSize); // ÅÌËã·ÖƬÊýÄ¿
let currentChunk = 0;

while (currentChunk < chunkCount) {
    const start = currentChunk * chunkSize;
    const end = (currentChunk + 1) * chunkSize;
    const blobChunk = file.slice(start, end); // »ñÈ¡Ä¿½ñ·ÖƬµÄBlob¹¤¾ß
    const formData = new FormData();

    formData.append('chunk', currentChunk);
    formData.append('chunks', chunkCount);
    formData.append('file', blobChunk);

    const xhr = new XMLHttpRequest();
    xhr.open('POST', '/upload', true);
    xhr.onload = function () {
        if (xhr.status !== 200) {
            console.error('ÎļþÉÏ´«Ê§°Ü');
            return;
        }
        const responseText = xhr.responseText;
        console.log(responseText);
        if (responseText === 'ÉÏ´«Íê³É') {
            console.log('ÎļþÉÏ´«ÀÖ³É');
        } else {
            console.log('ÕýÔÚÉÏ´«...');
        }
    };
    xhr.send(formData);

    currentChunk++;
}

µÇ¼ºó¸´ÖÆ

}

¶þ¡¢´óÎļþÏÂÔØ

ÔÚ´¦Àí´óÎļþÏÂÔØʱ£¬ÎÒÃÇÐèҪ˼Á¿ÒÔϼ¸¸ö·½Ã棺

ÎļþÏÂÔØʱҪ˼Á¿µ½Îļþ¾Þϸ£¬ÈôÊÇÎļþ½Ï´ó£¬ÐèҪʹÓ÷ֶÎÏÂÔØÊÖÒÕ£¬¼´·Ö¶Î¶ÁÈ¡ÎļþÄÚÈݲ¢·¢Ë͸ø¿Í»§¶Ë¡£Õâ¿ÉÒÔÓÐÓÃ×èÖ¹ÄÚ´æÍß½âµÄÎÊÌâ¡£

ÈôÊÇҪʵÏֶϵãÐø´«¹¦Ð§£¬ÐèÒª¼Í¼¿Í»§¶ËÒѾ­ÏÂÔصÄÎļþ¾Þϸ£¬ÔÚЧÀͶ˶ÁÈ¡ÎļþÄÚÈÝʱ£¬ÐèÒªÖ¸¶¨¶ÁÈ¡ÆðʼλÖúͳ¤¶È¡£

ÏÂÃæÊÇÒ»¸öʵÏÖ´óÎļþÏÂÔصĴúÂëʾÀý£º

// ÔÚ¿ØÖÆÆ÷Öнç˵´óÎļþÏÂÔØÒªÁì

public function download()

{

$filePath = './videos/big.mp4'; // ÒªÏÂÔصÄÎļþ·¾¶
$fileSize = filesize($filePath); // »ñÈ¡Îļþ¾Þϸ

header('Content-Disposition: attachment; filename="big.mp4"'); // ÉèÖÃÎļþÏÂÔØÃû×Ö
header('Content-Type: video/mp4'); // ÉèÖÃÎļþÀàÐÍ
header('Content-Length: ' . $fileSize); // ÉèÖÃÎļþ¾Þϸ

if (isset($_SERVER['HTTP_RANGE'])) { // ÈôÊÇÉèÖÃÁËHTTP_RANGE£¬ËµÃ÷ÊǶϵãÐø´«ÇëÇó
    header('HTTP/1.1 206 Partial Content');
    list($start, $end) = explode('-', $_SERVER['HTTP_RANGE']); // »ñÈ¡ÒѾ­ÏÂÔصÄÆðʼλÖúͿ¢ÊÂλÖÃ
    $start = max(0, intval($start));
    $end = min($fileSize - 1, intval($end));
    header('Content-Range: bytes ' . $start . '-' . $end . '/' . $fileSize);
    $length = $end - $start + 1;
} else { // ²»È»ÊÇÊ×´ÎÇëÇó
    $start = 0;
    $end = $fileSize - 1;
    $length = $fileSize;
}

header('Accept-Ranges: bytes');

$fp = fopen($filePath, 'rb');
fseek($fp, $start);

while (!feof($fp) && !connection_aborted() && $start <= $end) {
    set_time_limit(0);
    $buffer = fread($fp, min(1024 * 1024, $length)); // ·Ö¶Î¶ÁÈ¡ÎļþÄÚÈÝ
    echo $buffer;
    $start += strlen($buffer);
    $length -= strlen($buffer);
    flush(); // ɨ³ýÊä³ö»º´æ
}

fclose($fp);

µÇ¼ºó¸´ÖÆ

}

ÔÚÇ°¶ËÒ³Ã棬¿ÉÒÔʹÓÃJavaScriptʵÏÖ´óÎļþÏÂÔØ£º

function downloadFile(url) {

const request = new XMLHttpRequest();
request.open('GET', url, true);
request.onprogress = function (evt) {
    const progress = (evt.loaded / evt.total) * 100;
    console.log(`ÏÂÔؽø¶È: ${progress.toFixed(2)}%`);
};
request.send();

µÇ¼ºó¸´ÖÆ

}

×ÜÖ®£¬Ê¹ÓÃThinkPHP6£¬ÎÒÃÇ¿ÉÒÔ½ÏΪÀû±ãµØʵÏÖ´óÎļþÉÏ´«ºÍÏÂÔع¦Ð§£¬ÈÃÍøÕ¾Óû§¿ÉÒÔ¿ìËÙÀû±ãµØ¹²ÏíºÍ´«Êä´óÎļþ¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐдóÎļþÉÏ´«ºÍÏÂÔزÙ×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ