具有非必需文件上载的Php表单

时间:2015-07-03 23:37:52

标签: php forms file-upload

我正在尝试修复此表单,以便使文件上传成为一个选项,而不是必需的,因为我不会总是有可用的图像。到目前为止,这是我从研究中得出的结果,但我仍然收到一条消息,说文件是必需的。

    else if (isset($_POST['add_news_btn'])) {
    include 'connect.php';
    $newsdate = (isset($_POST['newsdate']) ? $_POST['newsdate'] : null);
    $newstitle = (isset($_POST['newstitle']) ? $_POST['newstitle'] : null);
    $newscatagory = (isset($_POST['newscategory']) ? $_POST['newscategory'] : null);
    $newstext = (isset($_POST['newstext']) ? $_POST['newstext'] : null);

    // upload file
    if (!empty($_FILES['newsuploader'])) {
        if(move_uploaded_file($_FILES["newsuploader"]["tmp_name"], "../media/images/" .$_FILES["newsuploader"]["name"]))
        echo "Saved";
        $imageURL = "media/images/" .$_FILES["newsuploader"]["name"];
/*  else
    $imageURL='';
 */

         if (isset($_POST['display']) && $_POST['display'] == '1')
        {
            $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 1)");
        }

        else {
            $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 0)");
        }

    }   else if (empty($_FILES['newsuploader'])){
        $imageURL = "media/images/news-logo.png";
        $stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 0)");

    }
    $stmt->bind_param('sssss', $newsdate, $newstitle, $newstext,     $newscatagory, $imageURL);
    $stmt->execute();
    $stmt->close();
    echo "done";
}

 }

对应表格

        <form name="news-page" action="" method="POST" enctype="multipart/form-data">
            <h1>News</h1>
            <span id="newstitle">
                <p id="newstitle">News Title</p>
                <input id="title" type="text" name="newstitle" value="News Title"/>
            </span>
            <span id="newsdate">
                <p>News Date</p>
                <input id="news_date" type="text" name="newsdate" value="News Date"/>               
            </span>
            <span id="category">
                <p>News Category</p>
                <input id="newscategory" type="text" name="newscategory" value="News Category"/>
            </span>
            <p id="news_info">News Information</p>
            <textarea id="newsinfo" name="newstext">Bacon ipsum dolor amet turducken boudin sirloin ..</textarea>
            <div id="newsimage">
                <img src/>
               <p>Insert News Image</p>
                    <label class="myLabel" id="news-image-upload">
                    <input type="file" required  name="newsuploader" id="fileToUpload" />
                    <span>Select Image</span>
                    </label>  
                    <input type="checkbox" name="display" value="1">Display Image
             <button type="submit" name="add_news_btn">Add News</button>
            </div>
            </form>

1 个答案:

答案 0 :(得分:0)

删除&#34; required&#34;归因于输入元素(在表单中)。

替换:

<input type="file" name="newsuploader" id="fileToUpload" />

使用:

self.cookies.update( requests.utils.dict_from_cookiejar(r.cookies) )