从目标路径检索上载的文件

时间:2018-08-25 10:57:09

标签: php html mysqli

我目前希望制作基本的上传和下载网络应用。我能够从目标' $ path '推入并上传文件,并标记' md5 ',但是我不确定如何通过下载链接或某种。

请原谅我的编码,我仍在学习)我已通过以下方式将文件保存到$ path目标:

<?php
include("connection.php");

if (isset($_POST['submit']))
{

$name = mysqli_real_escape_string($conn, $_POST['name']);
$description = mysqli_real_escape_string($conn, $_POST['description']);

if ($name == '' || $description == '' )
{

$error = 'ERROR: Please fill required fields!';

renderForm($name, $description);
}
else
{

if(isset($_FILES['filefield'])){
$file=$_FILES['filefield'];
$upload_directory='uploads/';
$ext_str = "gif,jpg,jpeg,mp3,tiff,bmp,doc,docx,ppt,pptx,txt,pdf";
$allowed_extensions=explode(',',$ext_str);
$max_file_size = 2097152;
$ext = substr($file['name'], strrpos($file['name'], '.') + 1);
if (!in_array($ext, $allowed_extensions) )
{

echo '<script language="javascript">';
echo 'alert("only gif,jpg,jpeg,png,mp4,tiff,bmp,doc,docx,ppt,pptx,txt,pdf files allowed to upload")';
echo '</script>';
exit(); 
}

$path=md5(microtime()).'.'.$ext;

if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){

    $filefield = $_FILES["filefield"]["name"];
    $path = $path."/".$filefield;
$query = "INSERT INTO `item`(`name`, `description`, `path`) VALUES ('$name','$description','$path')";
$result = mysqli_query($conn, $query);
 if($result)
    {
    echo '<script language="javascript">';
    echo 'alert("Item created!")';
    echo '</script>';      
    exit();
}   
}
}
}
}
?>

我已经设置了一个表以回显其内容(名称,描述,路径),还有一个按钮来查看相对于其链接“ id ”的每一行:

<?php
function renderForm($id, $name, $description, $path, $error)
{
?>
<html>
<body>
<div style="background-color:lightblue">
<strong>Name: </strong><?php echo $name; ?><br/>          
<strong>Description: </strong><?php echo $description; ?><br/>

<strong>Attachment: </strong><?php echo $path; ?><br/> <!--how to download?-->

</div>
</body>
</html>

<?php
include("connection.php");

$id = $_GET['id'];
$result = mysqli_query($conn, "SELECT * FROM order.item WHERE id=$id")
or die(mysqli_error());
$row = mysqli_fetch_array($result);

if($row)
{
$name = $row['name'];
$description = $row['description'];
$path = $row['path'];

renderForm($id, $name, $description, $path, '');
}
?>

DB:订单;表:项目 enter image description here

如何使它可点击下载?或通过其他任何方式来实现下载/检索?感谢帮助人员!

0 个答案:

没有答案