Changing a file upload script to allow a blank fil

2019-09-14 01:26发布

问题:

I'm working with a solution provided by Cool Hand Luke UK which upload's a file and add various details to MySQL (here's the post). So far, works great, but I want to change things a little.

My form is set-up to allow users to add either a file or a URL, but not both at once (code below, still some work to do to show/hide the URL and file fields but I'm sure you get the idea). I'd like to use the same script but run into issues when the file field is not populated.

I hoped someone could help tweak the php to allow me to have both functions using the same php?

Any help would be fantastic. Thanks in advance.

--

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:

<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:

In your PHP Script you can add the check to see if any file is uploaded or not.

For example see below snippet.

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

Hope this will Help

Thanks!

Hussain.