【问题标题】:How to pass variables from links in PHP?如何从 PHP 中的链接传递变量?
【发布时间】:2018-09-09 00:02:09
【问题描述】:

我用以下代码创建了一个页面 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");

}}

?>   

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

<?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

【问题讨论】:

  • header('Conetent-lenght: 中有拼写错误 - 您是从视频教程还是书籍中自己输入的?
  • 是的,老实说。我不熟悉 php !我可以设法在数据库中上传文件,但不能下载并逐步学习
  • 所有Conetent 标头前缀都应该是Content。您的 SQL 查询存在 SQL 注入漏洞,因此暂时不要发布。
  • No halfer,我没有保护。我只是好奇下载代码的架构是如何工作的,我是 PHP 的新手,我仍在逐步学习。我知道有时我会问愚蠢的问题 !但我希望有人能理解!谢谢你的关注。

标签: php database mysqli download


【解决方案1】:

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

<?php

包括“db.php”;

$id=$_GET['id'];

$query="SELECT*FROM test 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);

}

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

【讨论】:

    【解决方案2】:

    你忘了通过路径

    header("Location:index.php");
    

    应该是

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

    【讨论】:

    • 上传.php的路径头
    • 是的,福布斯,你是对的,我忘记了通过路径,非常感谢您的帮助!
    • @burimrexhepi 很好,如果它有助于标记它已回答请
    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 2017-10-29
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多