图片上传脚本无效

时间:2012-11-06 04:26:20

标签: php upload

我有一个名为addbanner.php的页面,其中包含用于上传图像的HTML和PHP脚本。 但它不适用于我。

*********************************addbanner.php***************************************
   <?php
   session_start();

require_once'session.php';
require_once '../cms/config.php';

if(isset($_POST['submit']))
{

//for image upload part

// define the posted file into variables
//$bannerid=$_POST['bannerid'];

 $name = $_FILES['file']['name'];
 $tmp_name = $_FILES['file']['tmp_name'];
 $type = $_FILES['file']['type'];
 $size = $_FILES['file']['size'];
 $desc=$_POST['description'];
 $dir='uploads/';
 $allowedExts = array("jpg", "jpeg", "gif", "png");
 $bits = (explode(".",$name ));
 $extension=end($bits);
 if ((($type == "image/gif")
 || ($type == "image/jpeg")
 || ($type == "image/png")
 || ($type == "image/pjpeg"))
  && ($size < 20000)
 && in_array($extension, $allowedExts))
 {
if ($_FILES["file"]["error"] > 0)
 {
  echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  }
 else
  {




 //connecting the database
  $object=new connection();
  $object=$object->dbConnect();
  $st=$object->prepare("select * from banners where  file=:name");
  //$st->bindParam(1,$bannerid);
   $st->bindValue(":name",$name,PDO::PARAM_STR);
  $st->execute();
  if($st->rowCount()>1)
  {
   echo "Error: Banner with this name is already exists!";
   }
    else
    {

   //$object=new connection();
  //$object=$object->dbConnect();
   $st=$object->prepare("insert into banners values('$name','$type','$size','$desc')");
   $st->execute();
     if (file_exists("uploads/" . $name))
         {
     echo $name. " already exists. ";
      }
     else
      {
        move_uploaded_file($tmp_name,$dir.$name);
           echo "Stored in: " . "uploads/" . $name;
        }
          }
      }
             }
      else
          {
             echo "Invalid file";
          }
              }
      else
           {
             //HTML part
           }
         ?>
********************javascript part***************
function validate()
{
if(document.myform.file.value=='' || document.myform.description.value=='')

{
alert("Please fill all the field!");

return false;


}

  }
******************end javascript***************

*******************html form*******************
        横幅图片(必填项)                    

                 

                                       说明(必填项)                                                                                             

                      
            <!-- End Form -->

                    <!-- Form Buttons -->
                    <div class="buttons" >
                      <input type="submit" name="submit" class="button" value="Submit" />
                      <input type="submit" name="cancel" class="button" value="Cancel" />
                    </div>
                    <!-- End Form Buttons -->
                </form>


********************end coding*****************

当我选择提交按钮时,它会输出“无效文件”,这也是脚本的一部分。 当我点击提交留下空白描述字段时,我的JavaScript中也存在问题。它有时不会显示警报。请提出建议。 我只想要当用户填写字段并单击“提交”时,它将转到数据库并保存在给定的文件夹中。

对于不完整的脚本感到抱歉,我还没有为取消按钮编码。

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码进行图片上传:

 $image =  $_FILES['image']['name'];

  $target_path = "uploads/";

    $target_path = $target_path . basename( $_FILES['image']['name']);

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path))
    {
           echo "The file ".  basename( $_FILES['image']['name'])." has been uploaded";

    }
    else
    {
            echo "There was an error uploading the file, please try again!";
    }

$query = "INSERT INTO imagetable values($image)";
mysql_query($query) or die(mysql_error());

查看此代码......

答案 1 :(得分:0)

尝试此操作 - 增加文件大小限制 - ($size < 20000)因为这会导致其他循环无效数据。