首页 / PHP / PHP Force下载产生损坏的文件
PHP Force下载产生损坏的文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP Force下载产生损坏的文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4328字,纯文字阅读大概需要7分钟。
内容图文
首先…我知道这个问题在此网站上已经讨论过很多次了,在过去的几个小时中,我一直在阅读评论和解决方案,但没有任何帮助.
我在这里发布的代码已经过修剪,但仍然包括我面临的问题.
我创建了一个小脚本来强制使用PHP下载.这只是我尝试在我的网站上使用的代码的一部分,因为我不想向您发送过多无关的代码,但是它仍然包含错误的输出.
此代码中的所有内容均使用10.6KB的.PNG文件进行了测试
注意:原始问题已解决,因此已删除.但是,当我将代码片段添加到我的网站时,遇到了另一个问题.
我创建了一个下载文件的功能:
<?php
function download_file($file)
{
$known_mime_types=array(
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"jpg" => "image/jpg",
"php" => "text/plain",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"pdf" => "application/pdf",
"txt" => "text/plain",
"html"=> "text/html",
"png" => "image/png",
"jpeg"=> "image/jpg"
);
if(!is_readable($file)) die('<p class="error">File not found or inaccessible!</p>');
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
$fsize = filesize($file);
header('Content-Type: ' .$mime_type);
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$fsize);
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Pragma: public');
header('Cache-Control:');
readfile($file);
exit();
}
?>
我从中调用函数的download.php:
<!DOCTYPE html>
<?php
require_once 'connect.inc.php';
require_once 'core.inc.php';
require_once 'download_file.php';
?>
<html>
<head>
<title>x3d Download</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css" type="text/css"/>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (loggedin())
{
include_once 'navbar_loggedin.php';
}
else
{
include_once 'navbar_loggedout.php';
}
?>
<div class="container" width="900px">
<h2>Downloads</h2>
<?php
$sql = "SELECT * FROM `files`";
$result = mysql_query($sql);
if (!$result)
{
echo '<p>No downloads available.</p>';
}
else
{
echo '<table class="table table-hover"><tr>';
echo '<tr><th>Filename</th>';
echo '<th>Filetype</th>';
echo '<th></th>';
if (loggedin())
{
if (getuserlevel($_SESSION['user_id']) == 'Administrator')
{
echo '<th></th>';
}
}
while($row = mysql_fetch_assoc($result))
{
echo '<tr><td><p>'.$row['file_name'].'</p></td>';
echo '<td><p>'.$row['file_type'].'</p></td>';
echo '<td><a href="download.php?download='.$row['file_id'].'"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span></a></td>';
if (loggedin())
{
if (getuserlevel($_SESSION['user_id']) == 'Administrator')
{
echo '<td><a class="red" href="download.php?delete='.$row['file_id'].'"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td>';
}
}
}
echo '</tr></table>';
}
?>
<?php
if (isset($_GET['download']))
{
$sql = "SELECT `file_name` FROM `files` WHERE `file_id`='".$_GET['download']."'";
if ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result);
$file = "uploads/" . $row['file_name'];
download_file($file);
}
}
if (isset($_GET['delete']))
{
$sql = "SELECT `file_name` FROM `files` WHERE `file_id`='".$_GET['delete']."'";
if ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result);
}
if ($row['file_name'] == "")
{
echo '<p class="error">File does not exist.</p>';
}
else
{
$filepath = "uploads/".$row['file_name'];
$sql = "DELETE FROM `files` WHERE `file_id`='".$_GET['delete']."'";
if (file_exists($filepath))
{
try
{
if (unlink($filepath))
{
if ($result = mysql_query($sql))
{
header('Location: download.php');
}
}
}
catch (Exception $e)
{
echo '<p class="error">Could not delete file.</p>';
}
}
}
}
?>
</div>
</body>
</html>
调用该函数的代码已经过测试,我的sql查询确实返回正确的值.
该图像包含我的html源代码的一部分和原始图像…
谁能帮我吗?
解决方法:
您的代码是好的.但是您下载的是一个致命错误,而不是图像:
<br />
<b>Fatal error</b>: Call to undefined function fileread() in <b>/var/www/html/test.php</b> on line <b>18</b><br />
更改fileread($file);与readfile($file);,它应该工作.
下次您有一个“ 140字节的损坏文件”时,请尝试将其作为文本文件打开.
内容总结
以上是互联网集市为您收集整理的PHP Force下载产生损坏的文件全部内容,希望文章能够帮你解决PHP Force下载产生损坏的文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。