图片上传器无法正常工作

时间:2015-07-14 09:28:06

标签: php sql file-upload

我正在尝试为网站构建图片上传器,但是只有选择了无图片的部分正在工作,当选择图片时,这会被忽略并且不会上传图片。试图找出错误但无法找到错误。

<?php
if (isset($_POST['submit_testimonial'])) {
$namesurname = (isset($_POST['namesurname']) ? $_POST['namesurname'] :   null);
$title = (isset($_POST['title']) ? $_POST['title'] : null);
$page = (isset($_POST['page']) ? $_POST['page'] : null);
$content = (isset($_POST['content']) ? $_POST['content']:null);
include 'connect.php';

if (empty($_FILES["test_image"]["name"])) {
$stmt = $conn->prepare("INSERT INTO testimonials (pageID, title, imageURL, content, client_name) VALUES (?, ?, 'media/images/smileydude.png', ?, ?)");
$stmt->bind_param('ssss',$page, $title, $content, $namesurname);
} 
if (!empty($_FILES["test_image"]["name"])) {
    if (move_uploaded_file($_FILES["test_image"]["tmp_name"], "../media/images/".$_FILES["test_image"]["name"]))
    $imageURL = "media/images/" .$_FILES["test_image"]["name"];
    $stmt = $conn->prepare("INSERT INTO testimonials (pageID, title, imageURL, content, client_name) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param('sssss', $page, $title, $imageURL, $content,  $namesurname);      
}
$stmt->execute();
$conn->close();
echo $imageURL;

}   
?>

Html代码

<form action="" method="post">
                    <div class="input-short">                   
                        <p>Name & Surname of Client</p>
                        <input name="namesurname" />
                    </div>
                    <div class="input-short">                   
                        <p>Company Name / Comment Title </p>
                        <input name="title"  id="company-name"/>
                    </div>
                    <div class="input-short">                   
                        <p>Client Type</p>
                        <select name='page'><option value=2>About</option>
 <option value=3>Brand Promotions</option>
  <option value=4>Corporate Services</option>
 <option value=5>Entertainment Services</option>
  <option value=6>After Glow Animation</option>
 </select>                      </div>
                    <div id="input-long">
                        <p>Content</p>
                        <textarea name="content"></textarea>
                    </div>
                    <div class="image-display"><img src="../media/images/smileydude.png"/>
                    <label class="myLabel" id="uploader">
                    <input type="file"  name="test_image" id="fileToUpload" />
                    <span>Select Image</span>
                    </label> </div>
                <button class="submit" type="submit" name="submit_testimonial">Submit Testimonial</button>
            </form>

1 个答案:

答案 0 :(得分:0)

更改您的表单
<form action="" method="post">

<form action="" method="post" enctype="multipart/form-data">

其中 enctype =“multipart / form-data”是文件上传所必需的