下载文件php程序显示错误

时间:2015-01-08 09:47:57

标签: php mysql

   // this php program showing no id was passed error, why its showing this error,
this program used for upload files into mysql database and list the files then create links for the uploaded file to download. but i had some problem, the download links shows error why?    

请帮助我解决这个问题...

listfile.php

    <html>
    <head>
    <title>Paging</title>
    </head>
    <body>
    <?php
    $dbhost = 'localhost';
    $dbuser = 'sathishcst';
    $dbpass = 'geni7joy';
    $rec_limit = 10;

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('gat-india.com');
    /* Get total number of records */
    $sql = "select*from enquiry_info,resume_file ";
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    $row = mysql_fetch_array($retval, MYSQL_NUM );
    $rec_count = $row[0];

    if( isset($_GET{'page'} ) )
    {
       $page = $_GET{'page'} + 1;
       $offset = $rec_limit * $page ;
    }
    else
    {
       $page = 0;
       $offset = 0;
    }
    $left_rec = $rec_count - ($page * $rec_limit);
    $sql = "select * from enquiry_info,resume_file where enquiry_info.id=resume_file.id\n"
        . "ORDER BY `resume_file`.`id` DESC limit $offset, $rec_limit";
    /*$sql = "SELECT id,name,email,phone ".
           "FROM pays ".
           "LIMIT $offset, $rec_limit";
    */
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
      echo '<table width="100%" border="1px" cellpadding="5px">
                    <tr>
                        <td><b>EmpName</b></td>
                        <td><b>Email</b></td>
                        <td><b>Mobile</b></td>
                        <td><b>Name</b></td>
                     <!--   <td><b>Mime</b></td>
                        <td><b>Size (bytes)</b></td>-->
                        <td><b>Created</b></td>
                        <td><b>Download resume</b></td>
                    </tr>';

    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo "
                    <tr>
                        <td>{$row['enqname']}</td>
                        <td>{$row['email']}</td>
                        <td>+91-{$row['phone']}</td>
                        <td>{$row['name']}</td>
                      <!--  <td>{$row['mime']}</td>
                        <td>{$row['size']}</td>-->
                        <td>{$row['created']}</td>
                        <td><a href='getfiles.php?id={$row['id']}'>{$row['name']}</a></td>
                    </tr>";
    } 
     echo '</table>';
    if( $page > 0 )
    {
       $last = $page - 2;
       echo "<hr><center><a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
       echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a></center><hr>";
    }
    else if( $page == 0 )
    {
       echo "<hr><br><center><a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
    }
    else if( $left_rec < $rec_limit )
    {
       $last = $page - 2;
       echo "<hr><br><center><a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a></center>";
    }
    mysql_close($conn);
    ?>


//    here the above list files working correctly... below only showing error.

getfile.php

    <?php
    // Make sure an ID was passed
    if(isset($_GET['id'])) {
    // Get the ID
        $id = intval($_GET['id']);

        // Make sure the ID is in fact a valid ID
        if($id <= 0) {
            die('The ID is invalid!');
        }
        else {
            // Connect to the database
            $dbLink = new mysqli('localhost', 'sathishcst', 'geni7joy', 'gat-india.com');
            if(mysqli_connect_errno()) {
                die("MySQL connection failed: ". mysqli_connect_error());
            }

            // Fetch the file information
            $query = "
                SELECT `mime`, `name`, `size`, `data`
                FROM `resume_file`
                WHERE `id` = {$id}";
            $result = $dbLink->query($query);

            if($result) {
                // Make sure the result is valid
                if($result->num_rows == 1) {
                // Get the row
                    $row = mysqli_fetch_assoc($result);

                    // Print headers
                    header("Content-Type: ". $row['mime']);
                    header("Content-Length: ". $row['size']);
                    header("Content-Disposition: attachment; filename=". $row['name']);

                    // Print data
                    echo $row['data'];
                }
                else {
                    echo 'Error! No image exists with that ID.';
                }

                / Free the mysqli resources
                @mysqli_free_result($result);
            }
            else {
                echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
            }
            @mysqli_close($dbLink);
        }
    }
    else {
        echo 'Error! No ID was passed.';
    }
    ?>

php程序显示no id was passed - 错误,为什么显示此错误?

此程序用于将文件上传到mysql数据库并列出文件,然后为上传的文件创建链接下载。但我有一些问题,下载链接显示错误原因?

1 个答案:

答案 0 :(得分:0)

您应该检查listfile.php

中getfile.php链接的文件名

<td><a href='getfiles.php?id={$row['id']}'>{$row['name']}</a></td>

并将其变为

<td><a href='getfile.php?id={$row['id']}'>{$row['name']}</a></td>

href文件与提供的文件不匹配。