2013-03-10 132 views
0
下面

的形式的代碼提交文本和/或上傳圖片PHP圖片上傳不起作用?

<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>'; 
} 

?> 

此代碼應插入該註釋並將上傳的圖像移動到圖像文件夾並將圖像路徑存儲在數據庫中,但是當用戶單擊提交時,他們的評論被插入而不是圖像路徑,並且該圖像不在臨時文件夾或圖像文件夾中,可以有人告訴我最近出了什麼問題?

+0

你得到一個錯誤信息? – 2013-03-10 01:24:08

+0

@ DanielA.White我得到一個解析錯誤,但它沒有任何信息,除了它的在線12 – 2013-03-10 01:35:03

回答

1

最高層級條件,你正在檢查的文件類型,可能永遠不會被觸發由於可能的錯字:$ _FILES [「new_image」] [「型」]

如果是這樣:$ _FILES [ '圖像類型'] ?

你也應該考慮抓住你的數據庫操作異常:

mysqli_query(..) or trigger_error(mysqli_error($dbc)); 
+0

我試過了,但我只是得到了第12行的解析錯誤,但多數民衆贊成它的所有信息 – 2013-03-10 01:48:16

+0

@RyanKelly進一步檢查。 。你有一個額外的')'::($ _FILES [「image」] [「type」] ==「image/pjpeg」))< - 刪除多餘的')'...那應該是'jpg '不'pjpeg'..代碼可能被其他拼寫錯誤所困擾。 – kalaero 2013-03-10 02:14:43