使用php echo语句下载文件
我的项目有问题。我有两个名为index.php和download.php的文件,其中index.php动态显示了表中的一些内容,所以我想下载像joe.pdf这样的表内容,因为我将单击我的链接,但我编写的代码显示了一些问题,比如无效文件,我想我在filedownloading echo语句中遗漏了一些内容 因此,我在此附上两个文件: index.php使用php echo语句下载文件,php,mysql,Php,Mysql,我的项目有问题。我有两个名为index.php和download.php的文件,其中index.php动态显示了表中的一些内容,所以我想下载像joe.pdf这样的表内容,因为我将单击我的链接,但我编写的代码显示了一些问题,比如无效文件,我想我在filedownloading echo语句中遗漏了一些内容 因此,我在此附上两个文件: index.php <?php // Establish Connection with Database $con = mysql_conne
<?php
// Establish Connection with Database
$con = mysql_connect("localhost","root");
// Select Database
mysql_select_db("cms", $con);
// Specify the query to execute
$sql = "select * from user_tbl where approval='0'";
// Execute query
$result = mysql_query($sql,$con);
$i = 1;
//echo "<form action='process.php' method='post'>";
echo "<table border='.025'>
<tr>
<th>UID</th>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Mobile</th>
<th>Email</th>
<th>gender</th>
<th>BirthDate</th>
<th>Username</th>
<th>Password</th>
<th>Station-Name</th>
<th>Verification Proof</th>
<th>Approve</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['User_Id'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>" . $row['BirthDate'] . "</td>";
echo "<td>" . $row['UserName'] . "</td>";
echo "<td>" . $row['Password'] . "</td>";
echo "<td>" . $row['Station_Name'] . "</td>";
//echo "<td>" . $row['VerificationProof'] . "</td>";
$name=$row['VerificationProof'];enter code here
echo "<td><a href='download.php?filename=$name'>$name</a></td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['User_Id']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
?>
<br>
</br>
<?php
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
?>
出现了什么错误?为什么要注释//echo“”代码>您的代码中有很多错误。例如:$name=$row['VerificationProof'];在此处输入代码
您应该使用锚链接将请求参数从index.php传递到download.php,或者使用表单。您在代码中留下了表单结束标记,在这种情况下,提交按钮甚至是不必要的。先生,我有一个这样的HTML语句,我想使用echo将其转换为相应的php语句,我尝试了如下echo“”;但是缺少了一些东西,所以它显示了一些错误,所以请帮助我如何将下面给出的html语句转换为php语句
<?php
function output_file($file, $mime_type='') {
if(!is_readable($file)) die('File not found or inaccessible!');
$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
"php" => "text/plain"
);
if($mime_type=='') {
$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="app`enter code here`lication/force-download";
};
};
@ob_end_clean();
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
if(isset($_SERVER['HTTP_RANGE'])) {
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024);
$bytes_send = 0;
if ($file = fopen($file, 'r')) {
if(isset($_SERVER['HTTP_RANGE']))
fseek($file, $range);
while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length)) {
$buffer = fread($file, $chunksize);
print($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else
die('Error - can not open file.');
die();
}
set_time_limit(0);
$file_path='Documents/'.$_REQUEST['filename'];
output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain');
?>