簡體   English   中英

如何使用PHP創建上傳文件的下載鏈接

[英]How to create a download links for uploaded files in PHP

我是PHP代碼的新手。 目前,我正在創建一個有關文件上載/下載的項目,用戶可以在該項目中登錄並上載帳戶中的文件。

我的問題是,如何使用戶上傳的文件可下載? 我是說,每個上傳文件的下載鏈接? 這里有人可以為我建議一個代碼嗎?

到目前為止,我已經成功地完成了上傳和列出成功上傳文件的代碼。 以下是已完成的代碼,這里的每個人都可以幫助我進行代碼下載功能。

    //code for listing uploaded files in "userpage.php"

    <div class="box6">
        <h3>File Lists</h3>
        <?php
        $username = $_SESSION['UserName'];
        if($handle = opendir('users/'.$username.'/')){
            while(false !== ($entry = readdir($handle))){
                if($entry != "." && $entry != ".."){
                    echo "$entry<br>";
                }
            }
            closedir($handle);
        }
        ?>
        <table width="650">
        <tr>
        <td>
        <?php echo $entry ;?>
        </td>
        </tr>
        </table>

    //code for uploading files after users press the upload button 

    <?php
    require("connection.php");
    session_start();
    $username = $_SESSION['UserName'];
    $udir= "users/".$username."/";
    $ufile = $udir . basename($_FILES['file']['name']);
    $file = ($_FILES['file']['name']);
    mysql_query("UPDATE `users` SET `Files` = '$file'") ; 
    if(move_uploaded_file($_FILES['file']['tmp_name'], $ufile)){
 header('location:uploadfiles.php?feedback3=uploadsuccessful');
    }
    else{
 header('location:uploadfiles.php?feedback3=uploaderror');
    }
    ?>

提前非常感謝您。

從PHP手冊: http//us1.php.net/fpassthru

<?php

// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

?>

如果您不想為每種文件類型提供一個,則可能希望將application/octet-stream用作Content-Type

同時提供Content-Disposition將強制瀏覽器顯示或下載圖像文件和其他可以顯示的文件。

警告: 請勿使用此工具-您將被黑客入侵。


t的簡單代碼可以下載所有文件,也可以下載任何文件,例如:images,excel,ppt,pdf,此代碼在圖像下載中的使用更為廣泛。.u可以非常輕松地上傳和下載。 您需要文件夾中的文件文件夾。 您可以將文件上載到文件夾中。您可以將文件夾中的文件文件夾中。 您需要數據庫來存儲上載文件名並下載以單擊文件名以自動下載。 我將鏈接到download.php文件,這是自動下載文件。 您無需更改下載文件..., 在php中上傳和下載文件

<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("demo",$conn) or die(mysql_error());
if(isset($_POST['submit'])!=""){
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
move_uploaded_file($temp,"files/".$name);
$insert=mysql_query("insert into upload(name)values('$name')");
if($insert){
header("location:index.php");
}
else{
die(mysql_error());
}
}
?>
<html>
<head>
<title>Upload and Download</title>
</head>
<style>
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
a{color:#666;}
#table{margin:0 auto;background:#333;box-shadow: 5px 5px 5px #888888;border-radius:10px;color:#CCC;padding:10px;}
#table1{margin:0 auto;}
</style>
<body>
<h2><a href="http://crackerworld.blogspot.in/">Cracker World</a></h2>
<h3><p align="center">Files Upload And Download</p></h3>
<form enctype="multipart/form-data" action="" name="form" method="post">
<table border="0" cellspacing="0" cellpadding="5" id="table">
<tr>
<th >Chosse Files</th>
<td ><label for="photo"></label><input type="file" name="photo" id="photo" /></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="submit" id="submit" value="Submit" /></th>
</tr>
</table>
</form>
<br />
<br />
<table border="1" align="center" id="table1" cellpadding="0" cellspacing="0">
<tr><td align="center">Click to Download</td></tr>
<?php
$select=mysql_query("select * from upload order by id desc");
while($row1=mysql_fetch_array($select)){
$name=$row1['name'];
?>
<tr>
<td width="300">
<img src="tick.png" width="14" height="14"><a href="download.php?filename=<?php echo $name;?>"><?php echo $name ;?></a>
</td>
</tr>
<?php }?>
</table>
</body>
</html>

下載.php

 <?php
function output_file($file, $name, $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="application/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='files/'.$_REQUEST['filename'];
output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain');
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM