更改文件上载脚本以允许空白文件字段

时间:2011-01-05 07:10:57

标签: php mysql file-upload

我正在使用Cool Hand Luke UK提供的解决方案,该解决方案上传了一个文件并向MySQL添加了各种详细信息(here's the post)。到目前为止,效果很好,但我想稍微改变一下。

我的表单设置为允许用户添加文件或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>

1 个答案:

答案 0 :(得分:0)

在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."; }
}

希望这会有帮助

谢谢!

侯赛因。