gpt4 book ai didi

php readfile() 和 fopen() 损坏/损坏下载

转载 作者:行者123 更新时间:2023-12-03 18:56:27 25 4
gpt4 key购买 nike

Long time lurker 第一次海报,

我已经完成了这个问题,所以我想我会放下骄傲并问专家,我在这里阅读了超过 20 个类似的问题并尝试了所有修复解决方案,但我得到了相同的结果。

我使用 PHP 供用户下载项目,但是我已经尝试过 readfile 和 fopen 但每次下载都是损坏的,有时大小为 0 有时是正确的(ish)大小,但总是损坏或损坏

谁能看一眼这段代码并告诉我它有什么问题,我已经搜索了很多次,现在我可能只是遗漏了一些非常简单的东西,......像往常一样

如有任何帮助,我们将不胜感激。

(如果需要可以显示/loap.php 的内容)

编辑:已解决对于偶然发现这个问题的人来说只是一个快速更新,我设法让这个工作......我已经尝试了某些命令的许多不同变体,但似乎我错过了尝试使用“ob_clean()”和“ob_end_flush()”与 fopen(), .. 它成功了,正如我所料,这是一个简单的修复 感谢 Twisty 的帮助,你激发了我的灵感 ;)

<?php 
require_once('../secura/load.php');

function get_remote_file_size($url, $readable = true){
$parsed = parse_url($url);
$host = $parsed["host"];
$fp = @fsockopen($host, 80, $errno, $errstr, 20);
if(!$fp) return false;
else {
@fputs($fp, "HEAD $url HTTP/1.1\r\n");
@fputs($fp, "HOST: $host\r\n");
@fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while(!@feof($fp))$headers .= @fgets ($fp, 128);
}
@fclose ($fp);
$return = false;
$arr_headers = explode("\n", $headers);
foreach($arr_headers as $header) {
$s = "Content-Length: ";
if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
$return = trim(substr($header, strlen($s)));
break;
}
}

return $return;
}

function get_ext($name)

{

$fn = get_basename($name);

return (strpos($fn, '.') ? strtolower(substr(strrchr($fn, '.'), 1)) : '');

}
function get_basename($name)



{

return basename(str_replace('\\', '/', $name));

}

function get_filesize_unit($size)



{

$size = max(0, $size);

static $u = array('&nbsp;B', 'KB', 'MB', 'GB');

for ($i=0; $size >= 1024 && $i < 4; $i++)

{

$size /= 1024;

}

return number_format($size, 1).' '.$u[$i];

}



///////////////////////////////////////////////////////////////////////////////////////////////

function find_mime_type($ext)

{

static $mime_types = array(

'application/andrew-inset' => array('ez'),

'application/mac-binhex40' => array('hqx'),

'application/mac-compactpro' => array('cpt'),

'application/mathml+xml' => array('mathml'),

'application/msword' => array('doc'),

'application/octet-stream' => array('bin', 'dms', 'lha',

'lzh', 'exe', 'class', 'so', 'dll', 'dmg'),

'application/oda' => array('oda'),

'application/ogg' => array('ogg'),

'application/pdf' => array('pdf'),

'application/postscript' => array('ai', 'eps', 'ps'),

'application/rdf+xml' => array('rdf'),

'application/smil' => array('smi', 'smil'),

'application/srgs' => array('gram'),

'application/srgs+xml' => array('grxml'),

'application/vnd.mif' => array('mif'),

'application/vnd.mozilla.xul+xml' => array('xul'),

'application/vnd.ms-excel' => array('xls'),

'application/vnd.ms-powerpoint' => array('ppt'),

'application/vnd.wap.wbxml' => array('wbxml'),

'application/vnd.wap.wmlc' => array('wmlc'),

'application/vnd.wap.wmlscriptc' => array('wmlsc'),

'application/voicexml+xml' => array('vxml'),

'application/x-bcpio' => array('bcpio'),

'application/x-cdlink' => array('vcd'),

'application/x-chess-pgn' => array('pgn'),

'application/x-cpio' => array('cpio'),

'application/x-csh' => array('csh'),

'application/x-director' => array('dcr', 'dir', 'dxr'),

'application/x-dvi' => array('dvi'),

'application/x-futuresplash' => array('spl'),

'application/x-gtar' => array('gtar'),

'application/x-hdf' => array('hdf'),

'application/x-javascript' => array('js'),

'application/x-koan' => array('skp', 'skd', 'skt', 'skm'),

'application/x-latex' => array('latex'),

'application/x-netcdf' => array('nc', 'cdf'),

'application/x-sh' => array('sh'),

'application/x-shar' => array('shar'),

'application/x-shockwave-flash' => array('swf'),

'application/x-stuffit' => array('sit'),

'application/x-sv4cpio' => array('sv4cpio'),

'application/x-sv4crc' => array('sv4crc'),

'application/x-tar' => array('tar'),

'application/x-tcl' => array('tcl'),

'application/x-tex' => array('tex'),

'application/x-texinfo' => array('texinfo', 'texi'),

'application/x-troff' => array('t', 'tr', 'roff'),

'application/x-troff-man' => array('man'),

'application/x-troff-me' => array('me'),

'application/x-troff-ms' => array('ms'),

'application/x-ustar' => array('ustar'),

'application/x-wais-source' => array('src'),

'application/xhtml+xml' => array('xhtml', 'xht'),

'application/xslt+xml' => array('xslt'),

'application/xml' => array('xml', 'xsl'),

'application/xml-dtd' => array('dtd'),

'application/zip' => array('zip'),

'audio/basic' => array('au', 'snd'),

'audio/midi' => array('mid', 'midi', 'kar'),

'audio/mpeg' => array('mpga', 'mp2', 'mp3'),

'audio/x-aiff' => array('aif', 'aiff', 'aifc'),

'audio/x-mpegurl' => array('m3u'),

'audio/x-pn-realaudio' => array('ram', 'ra'),

'application/vnd.rn-realmedia' => array('rm'),

'audio/x-wav' => array('wav'),

'chemical/x-pdb' => array('pdb'),

'chemical/x-xyz' => array('xyz'),

'image/bmp' => array('bmp'),

'image/cgm' => array('cgm'),

'image/gif' => array('gif'),

'image/ief' => array('ief'),

'image/jpeg' => array('jpeg', 'jpg', 'jpe'),

'image/png' => array('png'),

'image/svg+xml' => array('svg'),

'image/tiff' => array('tiff', 'tif'),

'image/vnd.djvu' => array('djvu', 'djv'),

'image/vnd.wap.wbmp' => array('wbmp'),

'image/x-cmu-raster' => array('ras'),

'image/x-icon' => array('ico'),

'image/x-portable-anymap' => array('pnm'),

'image/x-portable-bitmap' => array('pbm'),

'image/x-portable-graymap' => array('pgm'),

'image/x-portable-pixmap' => array('ppm'),

'image/x-rgb' => array('rgb'),

'image/x-xbitmap' => array('xbm'),

'image/x-xpixmap' => array('xpm'),

'image/x-xwindowdump' => array('xwd'),

'model/iges' => array('igs', 'iges'),

'model/mesh' => array('msh', 'mesh', 'silo'),

'model/vrml' => array('wrl', 'vrml'),

'text/calendar' => array('ics', 'ifb'),

'text/css' => array('css'),

'text/html' => array('html', 'htm'),

'text/plain' => array('asc', 'txt'),

'text/richtext' => array('rtx'),

'text/rtf' => array('rtf'),

'text/sgml' => array('sgml', 'sgm'),

'text/tab-separated-values' => array('tsv'),

'text/vnd.wap.wml' => array('wml'),

'text/vnd.wap.wmlscript' => array('wmls'),

'text/x-setext' => array('etx'),

'video/mpeg' => array('mpeg','3gp','mp4', 'mpg', 'mpe'),

'video/quicktime' => array('qt', 'mov'),

'video/vnd.mpegurl' => array('mxu', 'm4u'),

'video/x-msvideo' => array('avi'),

'video/x-sgi-movie' => array('movie'),

'x-conference/x-cooltalk' => array('ice')

);
foreach ($mime_types as $mime_type => $exts)
{
if (in_array($ext, $exts))
{
return $mime_type;
}
}
return 'text/plain';
}
$id = $_GET['id'];
$error = false;
$error = (!$product->is_product($id)?$products->error:$error);
$error =(!$purchases->is_purchased($_SESSION['uid'],$id)?$purchases->error:$error);
if(!$error){
$file = $product->details($id);
$filepath = $file['file'];
$fname=get_basename($filepath);
if (fopen($filepath,r) || (file_exists($filepath)) ){
if (@filesize($filepath)){
$fsize =filesize($filepath);
}
else
{
$fsize = get_remote_file_size($filepath);
}
$ext= get_ext($fname);
$ctype= find_mime_type($ext);
header('Content-Type:'. $ctype );
header('Content-Length: ' . $fsize);
header('Content-Disposition: attachment; filename=' . $fname);
ob_clean();
$file = fopen($filepath,'r');
ob_end_flush();
fpassthru($file);
set_time_limit(0);
}else{
return 'File Doesn\'t Exist'; } // exist fxn....
}
echo $error;
?>

最佳答案

对于偶然发现这个问题的人来说,只是一个快速更新,我设法让这个工作......

我尝试了某些命令的许多不同变体,但似乎我错过了使用 ob_clean() 和 ob_end_flush() 与 fopen() 的尝试,..它成功了,正如我所料,这是一个简单的修复

感谢 Twisty 的帮助,你激发了我的灵感 ;)

已在 OP 中更新了更正后的工作代码

关于php readfile() 和 fopen() 损坏/损坏下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com