我有一个名为 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 中也存在问题。它有时不显示警报。请提出一些建议。我只是想当用户填写该字段并单击提交时,它将进入数据库并保存在给定的文件夹中。
很抱歉脚本不完整,我还没有为取消按钮编写代码。