如何通过PHP中的链接传递变量?

时间:2018-09-09 00:02:09

标签: php database mysqli download

我用以下代码创建了一个页面indes.php:

    <?php

include "db.php";

$select="SELECT *FROM alldocs ";

$query=mysqli_query($conn,$select);

<form action="upload.php" method="POST" enctype="multipart/form-data">



 <input type="text" name="name" placeholder="name"><br><br>

 <input type="file" name="file" ><br><br>

 <input type="submit" name="submit" value="submit"><br><br>



</form>

<div class="box-info">

    <?php

        while($row=mysqli_fetch_assoc($query)){

            echo $row['name'].'<br>'.

            '<a href="downloud.php?get=$path">downloud file</a>    <br>';}

    ?>

</div>

然后我像这样创建了upload.php

<?php

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



include "db.php";

$name   =$_POST['name'];

$file   =$_FILES['file'];

$fileName   =$file['name'];

$fileTmp    =$file['tmp_name'];

$path='downloud/$fileName';

if(move_uploaded_file($fileTmp , $path)){

    $insert=" INSERT INTO alldocs(name,path) VALUES('$name','$path')";

    $query=mysqli_query($conn,$insert);

    header("Location:index.php");

}}

?>   

现在,我在链接中尝试连接到get = $ path的index.pho,以便可以进入下一页,这样便可以像这样从db下载路径:

<?php 

if(isset($_GET['get'])){ 



    include "db.php";

    $path=$_GET['get'];



    $select="SELECT*FROM alldocs WHERE path='$path';" ;

    $query=mysqli_query($conn,$select);



    header('Conetent-type:application/octet-stream');

    header('Conetent-disposition:attachment;filename="'.basename($path).'"' );

    header('Conetent-lenght:.filesize($path)'); 

    readfile($path);

}

?> 

因此,如果有人可以向我指出我在此代码中所犯的错误,因为我总是收到此错误WARNING: readfile($path); as undefined

2 个答案:

答案 0 :(得分:0)

我的代码的问题是我没有正确通过路径,所以对于:

<?php

包括“ db.php”;

$ id = $ _ GET ['id'];

$ query =“ SELECT * FROM测试WHERE id ='$ id'”;

$ result = mysqli_query($ conn,$ query);

while($ row = mysqli_fetch_assoc($ result)){

$path = $row['path'];   <--  //*getting the path *//-->

header('Content-Disposition:attachment; filename ='.$path.'');    <--// obviously i 
                                                               don't know if the 
                                                               basename is needed 
                                                               here *//-->
header('Content-type:application/octent-strem'); 
header('Content-lenght='.filesize($path)); 

readfile($path);

}

此代码可用于下载文件/图像!

答案 1 :(得分:-1)

您忘记了通过路径

header("Location:index.php");

应该是

header("Location:index.php?get=".urlencode($path));