错误显示图像 - 来自PHP&的代码MySQL新手到忍者

时间:2017-11-15 13:33:23

标签: php mysql image

我是PHP和MySQL的新手,还有Kevin Yank的书中的两章 - PHP& MySQL新手到Ninja代码中有错误。唯一一个我没有想到的就在于第12章,并且尝试了关于这个和其他论坛的多个帖子的建议,没有任何作用。预先感谢您的帮助

问题:Blob给加载带来了问题:

  

无法显示图像“http://localhost/chapter12/filestore5/index.php?action=view&id=5”,因为它包含错误

所有其他功能:上传,说明,删除工作完美。

index.php文件

var severeWeather: Results<SevereWeather>?
var vigiArray = Array<SevereWeather>()
var redCount: Int = 0

severeWeather = realm.objects(SevereWeather.self).filter(NSPredicate(format: "department != nil"))
.sorted(byKeyPath: "departmentNumber")

vigiArray = Array(severeWeather!)

redCount = vigiArray.filter { $0.dangerLevels.filter { $0.level.value == 4 }.count > 0 }.count

HTML文件

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';

if (isset($_POST['action']) and $_POST['action'] == 'upload') {

    // Bail out if the file isn't really an upload

    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
        $error = 'There was no file uploaded!';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

        exit();
    }

    $uploadfile = $_FILES['upload']['tmp_name'];
    $uploadname = $_FILES['upload']['name'];
    $uploadtype = $_FILES['upload']['type'];
    $uploaddesc = $_POST['desc'];
    $uploaddata = file_get_contents($uploadfile);
    include 'db.inc.php';

    try {
        $sql = 'INSERT INTO filestore SET
    filename = :filename,
    mimetype = :mimetype,
    description = :description,
    filedata = :filedata';
        $s = $pdo->prepare($sql);
        $s->bindValue(':filename', $uploadname);
        $s->bindValue(':mimetype', $uploadtype);
        $s->bindValue(':description', $uploaddesc);
        $s->bindValue(':filedata', $uploaddata);
        $s->execute();
    }

    catch(PDOException $e) {
        $error = 'Database error storing file!';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

        exit();
    }

    header('Location: .');
    exit();
}

if (isset($_GET['action']) and ($_GET['action'] == 'view' or $_GET['action'] == 'download') and isset($_GET['id'])) {
    include 'db.inc.php';

    try {
        $sql = 'SELECT filename, mimetype, filedata
    FROM filestore
    WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_GET['id']);
        $s->execute();
    }

    catch(PDOException $e) {
        $error = 'Database error fetching requested file.';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

        exit();
    }

    $file = $s->fetch();
    if (!$file) {
        $error = 'File with specified ID not found in the database!';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

        exit();
    }

    $filename = $file['filename'];
    $mimetype = $file['mimetype'];
    $filedata = $file['filedata'];
    $disposition = 'inline';
    if ($_GET['action'] == 'download') {
        $mimetype = 'application/octet-stream';
        $disposition = 'attachment';
    }

    // Content-type must come before Content-disposition

    header('Content-length: ' . strlen($filedata));
    header("Content-type: $mimetype");
    header("Content-disposition: $disposition; filename=$filename");
    echo $filedata;
    exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'delete' and isset($_POST['id'])) {
    include 'db.inc.php';

    try {
        $sql = 'DELETE FROM filestore
    WHERE id = :id';
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->execute();
    }

    catch(PDOException $e) {
        $error = 'Database error deleting requested file.';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

        exit();
    }

    header('Location: .');
    exit();
}

include 'db.inc.php';

try {
    $result = $pdo->query('SELECT id, filename, mimetype, description
    FROM filestore');
}

catch(PDOException $e) {
    $error = 'Database error fetching stored files.';
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';

    exit();
}

$files = array();

foreach($result as $row) {
    $files[] = array(
        'id' => $row['id'],
        'filename' => $row['filename'],
        'mimetype' => $row['mimetype'],
        'description' => $row['description']
    );
}

include 'files.html.php';

?>

1 个答案:

答案 0 :(得分:0)

最后回到修复代码的地方,在这里找到了可行的解决方案: https://www.sitepoint.com/community/t/problem-using-php-to-pull-binary-files-from-a-blob-field-in-mysql/6431/16 我添加了“ while(@ob_end_clean());”在index.php中的magicquotes之后,并且一切正常。 根据此人在另一个论坛上发现的信息,如果服务器打开了输出缓冲,则它将无法正确发送图像数据。