更改文件上传脚本,让一个空白文件场(Changing a file upload script to

2019-10-30 08:56发布

我与铁窗喋血英国提供了一个解决方案,上传工作的一个文件,并添加各种细节到MySQL( 这里的职位 )。 到目前为止,工程巨大,但我想改变的东西一点点。

我的形式建立,以允许用户在一次(代码添加文件或URL,但不能同时下面,仍然有一些工作要做,以显示/隐藏URL和文件领域,但我敢肯定你的想法)。 我想使用相同的脚本,但遇到问题不会填充文件字段时。

我希望有人可以帮助调整了PHP中使用相同的PHP让我有两种功能?

任何帮助将是非常美妙的。 提前致谢。

-

PHP(insert_news.php):

    <?php error_reporting(E_ALL^E_NOTICE);

    define('INCLUDE_CHECK',true); include "connect.php";

    $target = "../uploads/"; $target = $target . basename( $_FILES['photo']['name']);

    //This gets all the other information from the form 
    $name=$_POST['name']; 
    $author=$_POST['author'];
    $newsID=$_POST['newsID']; 
    $description=$_POST['description']; 
    $type=$_POST['type']; 
    $url=$_POST['url']; 
    $pic=(mysql_real_escape_string($_FILES['photo']['name']));

    //Writes the information to the database 
    mysql_query("INSERT INTO adidas_news (name, description, documentName, author, newsID, url, type) VALUES ('$name', '$description', '$pic', '$author', '$newsID', '$url', '$type')");

    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {

    //Tells you if its all ok 
    header("Location: ../news.php"); } else {

    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."; }

    ?>

形成:

<form method="post" id="addURLForm" action="insert_news.php">
                <img src="images/bg_addform_top.gif" width="446" height="11" />
                    <table class="addForm" cellspacing="0">
                      <tr>
                        <th>Name:<span class="greenText">*</span></th>
                        <td><input name="name" type="text" class="textBox required" maxlength="80" /></td>
                      </tr>
                      <tr>
                        <th>Type:<span class="greenText">*</span></th>
                        <td>
                            <select name="type" class="textBox">
                                <option selected="selected">-- Select --</option>
                                <option value="file">File/Document</option>
                                <option value="url">URL/Link</option>
                            </select>
                        </td>
                      </tr>
                      <tr>
                        <th>Full URL:<span class="greenText">*</span></th>
                        <td><input name="url" type="text" class="textBox url" maxlength="120" /></td>
                      </tr>
                      <tr>
                        <th>File:<span class="greenText">*</span></th>
                        <td><input type="file" class="" name="photo"></td>
                      </tr>
                      <tr>
                        <th>Description:<span class="greenText">*</span></th>
                        <td><textarea name="description" class="textBox required"></textarea></td>
                      </tr>
                      <tr>
                        <th>
                            <input name="newsID" type="hidden" value="<?php
                            for ($i=0; $i<5; $i++) {
                            $d=rand(1,30)%2;
                            echo $d ? chr(rand(65,90)) : chr(rand(48,57));
                            }
                            ?>" />
                            <input name="author" type="hidden" value="<?php             
                            $result = mysql_query("SELECT * FROM adidas_members WHERE usr='".$_SESSION['usr']."'");
                            while($row = mysql_fetch_array($result))
                            {
                            echo "" . $row['fullName'] . "";
                            }
                            ?>" />
                        </th>
                        <td><input type="image" class="submitButton" src="images/button_submit.gif" /></td>
                      </tr>
                    </table>

            </form>

Answer 1:

在你的PHP脚本,您可以添加检查,看是否有文件被上传或不。

例如见下文片段。

if(isset($_FILES['photo']['tmp_name'])) //  check if any file is uploaded
{

   //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {

   //Tells you if its all ok header("Location: ../news.php"); } else {

   //Gives and error if its not echo "Sorry, there was a problem uploading your file."; }
}

希望这将有助于

谢谢!

侯赛因。



文章来源: Changing a file upload script to allow a blank file field