dongqiu3709 2015-07-14 09:28
浏览 33
已采纳

图片上传器无法正常工作

I'm trying to build an image uploader for a website, however only the part where the no image is selected is working, when an image is selected, this is ignored and no image is uploaded. Tried to figure out what is wrong but can't locate the error.

<?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 code

<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条回答 默认 最新

  • dsepcxw181184853 2015-07-14 09:32
    关注

    Change your form from

    <form action="" method="post">
    

    to

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

    where enctype="multipart/form-data" is necessary for file uploads

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?