0

以下は、テキストを送信および/または画像をアップロードするためのフォームのコードです

<form id="comments" action="insertcomment.php" method="POST" enctype="multipart/form-data">
    Comment: <input type="text" name="comment" id="commentfield">
    <br>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Image URL (Limit: 1MB): <input type="file" name="image">
    <br>
    <input type="submit" name="submit" value="Post comment" class="btn btn-primary">
    </form>

ユーザーが送信ボタンをクリックすると、このphpスクリプトに移動します

<?php

include('../c_database.php');

$timeSet = date_default_timezone_set("Europe/London");

$User = $_COOKIE['username'];
$comments = mysqli_real_escape_string($dbc, $_REQUEST['comment']);
$time = date(DATE_RFC822);

        if($_FILES['image']['size'] <= 1048576){

$allowedExts = array("jpg", "jpeg", "gif", "png", "bmp", "tiff", "xtiff");

$extension = end(explode(".", $_FILES["image"]["name"]));

        if ((($_FILES["image"]["type"] == "image/gif")

        || ($_FILES["image"]["type"] == "image/jpeg")

        || ($_FILES["new_image"]["type"] == "image/png")

        || ($_FILES["image"]["type"] == "image/pjpeg"))

        || ($_FILES["image"]["type"] == "image/bmp"))

        && in_array($extension, $allowedExts))

{

if ($_FILES["image"]["error"] > 0) {

$error_message = $_FILES["image"]["error"];

} else {

if (file_exists("images/" . $_FILES["image"]["name"]))

{

$error_message = $_FILES["image"]["name"] . " " . $LANG['image_exist'];

} else {

if(move_uploaded_file($_FILES["image"]["tmp_name"], "images/" . $_FILES["image"]["name"])) {

// success
$image_name = $_FILES["image"]["name"];

} else {

$error_message = "Upload Failed!";

}

}

}

} else {

$error_message = "Error: May be different ext or size";

}

}

$imagepath = 'images/'. $_FILES["image"]["name"];

$commentQuery = "INSERT INTO comments (username, comments, time_added, imagepath) VALUES ('$User' ,'$comments' ,'$time' ,'$imagepath')";
$executeCommentQuery = mysqli_query($dbc, $commentQuery);

if($executeCommentQuery){

$user = $_COOKIE['username'];

$commentsMadeUpdate = "UPDATE login SET Comments_Made = Comments_Made +1 WHERE Username='$user'";
$executeUpdateQuery = mysqli_query($dbc, $commentsMadeUpdate);

 echo '<!DOCTYPE html> 
 <html>
 <head>
<title>Comment</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js">      </script>
</head>

<body>';
  echo 'Comment and/or image uploaded successfully';

  echo '<script>location.href="comments.php"</script>';

  echo '
 </body>
 </html>';

 } else {

echo '<!DOCTYPE html> 
<html>
<head>
    <title>Comment</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js">    </script>
</head>

<body>';
  printf("Errormessage: %s\n", mysqli_error($dbc));
echo '
</body>
</html>';
}

?>

このコードはコメントを挿入し、アップロードされた画像を画像フォルダーに移動し、画像パスをデータベースに保存する必要がありますが、ユーザーが送信をクリックすると、コメントは挿入されますが画像パスは挿入されず、画像は一時フォルダーまたは画像フォルダ、誰かが何がうまくいかないのか教えてもらえますか?

4

1 に答える 1

1

ファイルタイプをチェックしている最高レベルの条件は、タイプミスの可能性があるため、トリガーされない可能性があります: $_FILES["new_image"]["type"]

$_FILES['image']['type'] ?

データベース操作の例外をキャッチすることも検討する必要があります。

mysqli_query(..) or trigger_error(mysqli_error($dbc));
于 2013-03-10T01:30:33.157 に答える