更改文件上载脚本以允许空白文件字段 [英] Changing a file upload script to allow a blank file field

查看:150
本文介绍了更改文件上载脚本以允许空白文件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由Cool Hand Luke UK提供的解决方案,该解决方案上传了一个文件,并向MySQL添加了各种细节(这里的职位)。到目前为止,效果很好,但是我想稍微改变一下。

我的表单设置为允许用户添加文件或URL,但不能(下面的代码,仍然有一些工作要做,以显示/隐藏的URL和文件的领域,但我相信你明白了)。我想使用相同的脚本,但遇到问题时,文件字段未填充。

我希望有人可以帮助调整PHP,让我有两个函数使用相同的PHP?



任何帮助将是太棒了。


PHP(insert_news.php):

 <?php error_reporting(E_ALL ^ E_NOTICE); 

define('INCLUDE_CHECK',true);包括connect.php;

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

//这将从表单
$ 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']));
$ b $ //将信息写入数据库
mysql_query(INSERT INTO adidas_news(name,description,documentName,author,newsID,url,type)VALUES('$ name','$描述','$ pic','$ author','$ newsID','$ url','$ type'));
$ b $ //将照片写入服务器
if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target)){

//告诉你是否全部ok
头(Location:../news.php); } else {

//给出错误,如果它不是
echo对不起,上传文件时出现问题。 }

?>

表格:

 < form method =postid =addURLFormaction =insert_news.php> 
< img src =images / bg_addform_top.gifwidth =446height =11/>
< table class =addFormcellspacing =0>
< tr>
< th>名称:< span class =greenText> *< / span>< / th>
< td>< input name =nametype =textclass =textBox requiredmaxlength =80/>< / td>
< / tr>
< tr>
< th>类型:< span class =greenText> *< / span>< / th>
< td>
= rand(1,30)%2;
echo $ d?chr(rand(65,90)):chr(rand(48,57));
}
?> />
< input name =authortype =hiddenvalue =<?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 =imageclass =submitButtonsrc =images / button_submit.gif/>< / td>
< / tr>
< / table>

< / form>


解决方案

在您的PHP脚本中,您可以添加支票如果有任何文件上传或不。

例如下面的代码片段。

  {

//将照片写入服务器if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target)){

//告诉你它的所有ok标题(Location:../news.php ); }其他{

//给出错误,如果不回显对不起,上传文件时出现问题。 }
}

希望这可以帮助

谢谢!

Hussain

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>

解决方案

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.

这篇关于更改文件上载脚本以允许空白文件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆