【问题标题】:Upload with PHP使用 PHP 上传
【发布时间】:2017-02-13 08:07:54
【问题描述】:

我正在创建一个网站,并且我有一个表单来添加新帖子。我正在使用 iFrame 而不是 ajax 来全面支持 IE(好吧,我知道 IE 是最讨厌和最差的浏览器之一,但有些菜鸟正在使用它)。基本上,当我提交表单时,它给了我一个内部错误(500)。 这是我的表格:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.3.2/css/ionic.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form enctype="multipart/form-data" class="form" id="form" action="./addPost.php" method="POST">
         <div class="list">
          <label class="item item-input">
            <input type="text" placeholder="Title" class="AddPosttitle" name="title">
          </label>
          <label class="item item-input">
            <input class="description" type="text" placeholder="Simple Description (max 60 caracters)" maxlength="60" name="description">
          </label>
          <label class="item item-input">
            <div>
                <span id='button_upload'>Image : </span>
                <input type='file' class="img" name="img">
            </div>
          </label>
          <label class="item item-input">
            <textarea placeholder="Full description" class="full" name="full"></textarea>
          </label>
            <div class="padding">
              <button class="button button-block button-positive submit-btn" type="submit">
               Submit
            </button>
            </div>
        </div>
    </form>
    <style type="text/css">
        .form {
            background: #FFF;

        }
    </style>
    <?php
    if (!empty($_GET['error'])){
        ?>
        <script type="text/javascript">
            function findGetParameter(parameterName) {
                var result = null,
                    tmp = [];
                var items = location.search.substr(1).split("&");
                for (var index = 0; index < items.length; index++) {
                    tmp = items[index].split("=");
                    if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
                }
                return result;
            }
            alert(findGetParameter("error"));
        </script><?php
    }
    ?>

我正在使用 Ionic 库,只是因为我喜欢某些组件,但无论如何,这是我的 addPost.php 文件:

<?php
try
{
   $db = new PDO('mysql:host=something;dbname=someDB;charset=utf8', 'ID', 'LOL');
}
catch(Exception $e)
{
   die('Erreur : '.$e->getMessage());
}

$post = $_POST;
$img = base64_encode(file_get_contents($_FILES['img']['tmp_name']));
$title = addslashes($post['title']);
$description = addslashes($post['description']);
$fullDesc = addslashes($post['full']);
// if (!empty($title) & !empty($description) & !empty($fullDesc) & !empty($img)) {

// }
// else {
//  // header("Location: form.php?error=Fill the form!");
// }

$sql = "INSERT INTO posts (title, description, img, fullDesc, likes) VALUES ('$title', '$description', hextoraw('$img')', '$fullDesc', 0)";
$db->exec($sql);
    // header("Access-Control-Allow-Origin: *");
header("Location: form.php?error=$sql");

我不知道为什么会出现错误。我认为它来自图像,但我不确定。我仍在尝试最终的解决方案,如果我发现了什么,我会编辑我的问题(只是为了让你知道!)

【问题讨论】:

  • 在插入帖子中有一个额外的引用。只是为了让你知道。
  • 无论hextoraw 是什么,您都不能从字符串中调用函数。

标签: javascript php apache upload


【解决方案1】:

您确实应该使用prepared statements 来保证安全性:

$stmt = $db->prepare("INSERT INTO posts (title, description, img, fullDesc, likes) VALUES (:title, :description, :img, :fullDesc, :likes)");

$stmt->bindParam(':title', $title);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':img', hextoraw($img));
$stmt->bindParam(':fullDesc', $fullDesc);
$stmt->bindParam(':likes', 0);

$stmt->execute();

这还可以帮助您创建易于重用的语句(请参阅链接)。

【讨论】:

  • 好吧,我有一个意想不到的问题,它给了我这个:Fatal error : Uncaught Error: Cannot pass parameter 2 by reference in /api/addPost.php:30 Stack Trace #0 {main} thrown is /api/addPost.php on the line 30 第 30 行是 $stmt-&gt;bindParam(':likes', 0);
【解决方案2】:

您在 sql 字符串中打印错误: hextoraw('$img')'

删除最后一个引号

【讨论】:

    【解决方案3】:

    如果显示的 500 错误是一般错误页面(没有任何堆栈跟踪或详细的错误消息),请查找您的网络服务器的错误日志文件的最后几行,并将它们发布在此处。我们需要有关您遇到的错误的更多信息。也许是文件访问错误?数据库错误?

    另外,如果您使用 HEXTORAW Oracle 函数将十六进制字符串转换为二进制数据,您必须注意 HEXTORAW 使用的是十六进制字符串,而不是 base 64 编码的字符串。 您应该使用 http://php.net/manual/en/function.bin2hex.php 而不是 base64_encode。

    【讨论】:

      猜你喜欢
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2010-09-26
      • 2011-03-17
      相关资源
      最近更新 更多