0

我不确定这段代码有什么问题,所以我希望能提供一些有用的输入。

问题

它不会获取我上传的文件的名称,也不会实际将其上传到定义的目录中。

PHP 代码

<? include("header.php"); 
include("sidebar.php");
?>        



    <h2>Add Raffle</h2> 

 <?
$name = asql($_POST['name']);
$type = asql($_POST['prize']);
$min_points = asql($_POST['minp']);
$min_cash = asql($_POST['maxu']);
$cons = strtotime($_POST['cons']);
$cone = strtotime($_POST['cone']);


if($_POST['subm'])
{
if ($name == NULL OR $type == NULL OR $min_points == NULL OR $min_cash == NULL) {
print"Please Fill Out All Of The Required Fields<br /><a href='rafadd.php'><img src='img/green_arrow_left.png' alt='Go Back' /></a>"; include"footer.php";
exit();
}
if (!in_array($file['type'], array("image/gif", "image/jpeg", "image/png"))
           || $file['size'] > 20000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "There was an error";
    }
  else
    {
    echo "";
$filename = $_FILES["file"]["name"];
    if (file_exists("../images/rewards/" . $_FILES["file"]["name"]))
      {
unlink("../images/raffles/" . $_FILES["file"]["name"]);
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/raffles/" . $_FILES["file"]["name"]);
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../images/raffles/" . $_FILES["file"]["name"]);
      }
    }
  }
else
  {
  echo "Invalid file";


$updatecontests = mysql_query("INSERT INTO raffles (`id`, `raffle_name`, `raffle_prize`, `buy_in`, `max_entry`, `prize_image`, `start`, `end`) VALUES ('','$name','$type','$min_points','$min_cash','images/raffles/".$filename."','$cons','$cone')") or die(mysql_error());

if($updatecontests){
$create = mysql_query("CREATE TABLE IF NOT EXISTS `".$name."` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` text NOT NULL,
  `entries` int(11) NOT NULL,
  PRIMARY KEY (`username`(30)),
  KEY `id` (`id`)
)");
}
print "You Have Successfully Added this Raffle<br /><br />
<a href='rafadd.php'><img src='img/green_arrow_left.png' alt='Go Back' /></a>"; 




    }
    else
    {

print"<div class='form'>
         <form action='' method='post'><input type=hidden name=subm value=1>";
?>
         <div class="element">
                        <label for="name">Raffle Name: <span class="red">(required)</span></label>
                        <input id="name" name="name" size="50" />
                    </div>
         <div class="element">
                        <label for="prize">Raffle Prize: <span class="red">(required)</span></label>
                        <input id="prize" name="prize" size="50" />
                    </div>
<div class='element'>
                        <label for='file'>Prize Image:</label>
                        <input type='file' name='file' id='file' />
                    </div>

                    <div class="element">
                        <label for="cons">Start Date: <span class="red">(required)</span></label>
                        <input id="cons" name="cons" size="50" /><a href="javascript:NewCal('cons','mmddyyyy',true,12)"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                        </div>
                    <div class="element">
                        <label for="cone">End Date: <span class="red">(required)</span></label>
                        <input id="cone" name="cone" size="50" /><a href="javascript:NewCal('cone','mmddyyyy',true,12)"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                        </div>
                    <div class="element">
                        <label for="minp">Cost Per Ticket: <span class="red">(required)</span></label>
                        <input id="minp" name="minp" size="50" />
                        </div>
                    <div class="element">
                        <label for="maxu">Maximum Entries per User: <span class="red">(required 0 for unlimited)</span></label>
                        <input id="maxu" name="maxu" size="50" />
                        </div>
<?php
                    print" <dl class='submit'>
                    <input type='submit' name='subm' id='subm' value='Submit' />
                     </dl>


         </form>
         </div> "; 
      }
      ?>

     </div><!-- end of right content-->


  </div>   <!--end of center content -->               




    <div class="clear"></div>
    </div> <!--end of main content-->


    <div class="footer">

        <div class="left_footer">IN ADMIN PANEL | Powered by <a href="http://indeziner.com">INDEZINER</a></div>
        <div class="right_footer"><a href="http://indeziner.com"><img src="images/indeziner_logo.gif" alt="" title="" border="0" /></a></div>

    </div>

</div>      
</body>
</html>

我在另一个脚本中使用了文件上传片段,它上传完美,所以我不确定为什么它在这里不起作用。

4

3 回答 3

5

在表单标签中添加 enctype="multipart/form-data"

<form action='' method='post'enctype="multipart/form-data">
于 2012-11-20T01:27:15.857 回答
1
<form action='' method='post'>

错了,应该是:

 <form action='' method='post' enctype='multipart/form-data'>

另外,使用<?php代替<?

PS我错过了设置 $file 的地方。

于 2012-11-20T01:27:24.000 回答
1

它看起来不像文件在任何地方被实例化。你的意思是:

if (!in_array($_FILES['file']['type'], array("image/gif", "image/jpeg", "image/png"))
       || $_FILES['file']['size'] > 20000)
于 2012-11-20T01:30:03.423 回答