dsfd3546 2018-07-31 05:17
浏览 85
已采纳

获取错误:类mysqli的对象无法转换为字符串

I got an Unique Download Link Generator from: here and started to set it up on my server, but if I try to open the download.php, I get the error:

Object of class mysqli could not be converted to string in filepath on line 18.

The Problem is, that the code was made for PHP5 but I'm using PHP7 so there are some changes between the versions and I'm not able to find them and fix the problem.

My Code:

<?php

// Set the maximum number of downloads
$maxdownloads = "2";
// Set the key's viable duration in seconds (86400 seconds = 24 hours)
$maxtime = "86400";

require ('dbconnect.php');

if(get_magic_quotes_gpc()) {
    $id = stripslashes($_GET['id']);
}else{
    $id = $_GET['id'];
}

// Get the key, timestamp, and number of downloads from the database
$query = sprintf($link, "SELECT * FROM downloadkey WHERE uniqueid=  '%s'",mysqli_real_escape_string($link, $id));
$result = mysqli_query($query) or die(mysqli_error());
$row = mysqli_fetch_array($result);
if (!$row) { 
    echo "The download key you are using is invalid.";
}else{
    $timecheck = date('U') - $row['timestamp'];

    if ($timecheck >= $maxtime) {
        echo "This key has expired (exceeded time allotted).<br />";
    }else{
        $downloads = $row['downloads'];
        $downloads += 1;
        if ($downloads > $maxdownloads) {
            echo "This key has expired (exceeded allowed downloads).<br />";
        }else{
            $sql = sprintf("UPDATE downloadkey SET downloads = '".$downloads."' WHERE uniqueid= '%s'",mysqli_real_escape_string($id, $link));
            $incrementdownloads = mysqli_query($sql) or die(mysqli_error());

// Debug        echo "Key validated.";

// Force the browser to start the download automatically

/*
Variables: 
    $file = real name of actual download file on the server
    $filename = new name of local download file - this is what the visitor's        file will actually be called when he/she saves it
*/

 ob_start();
 $mm_type="document/text";
 $file = "text.txt";
 $filename = "text.txt";

 header("Cache-Control: public, must-revalidate");
 header("Pragma: no-cache");
 header("Content-Type: " . $mm_type);
 header("Content-Length: " .(string)(filesize($file)) );
 header('Content-Disposition: attachment; filename="'.$filename.'"');
 header("Content-Transfer-Encoding: binary
");

 ob_end_clean();
 readfile($file);

        }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • duandun2218 2018-07-31 05:38
    关注

    I can see two problems in your code. Firstly, on this line:

    $query = sprintf($link, "SELECT * FROM downloadkey WHERE uniqueid=  '%s'",mysqli_real_escape_string($link, $id));
    

    $link should not be an input to sprintf. This is what is causing the error message that you are seeing. Change it to:

    $query = sprintf("SELECT * FROM downloadkey WHERE uniqueid=  '%s'",mysqli_real_escape_string($link, $id));
    

    Also, on this line:

    $sql = sprintf("UPDATE downloadkey SET downloads = '".$downloads."' WHERE uniqueid= '%s'",mysqli_real_escape_string($id, $link));
    

    the order of parameters to mysqli_real_escape_string is incorrect (it should be $link, $id), so change that line to:

    $sql = sprintf("UPDATE downloadkey SET downloads = '".$downloads."' WHERE uniqueid= '%s'",mysqli_real_escape_string($link, $id));
    

    You also need to change this line:

    $incrementdownloads = mysqli_query($sql) or die(mysqli_error());
    

    to

    $incrementdownloads = mysqli_query($link, $sql) or die(mysqli_error($link));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化