我在下面的表单中有一个取消按钮,如果用户正在上传文件,他们可以点击“取消”按钮取消上传,如果他们愿意的话。但我的问题是,无论文件是在上传过程中,它总是在包含文件名的数据库中插入一行。我想要的是,如果现有的或新的文件上传到服务器的“ImageFiles”文件夹,然后插入包含文件名的数据库行,否则如果取消上传,则不插入数据库行,这可能吗? / p>
以下是当前的表单代码:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'></p>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
下面是imageupload.php脚本,它上传文件以及插入数据库行的位置:
<?php
session_start();
...//Connect to DB
$result = 0;
if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
mysql_close();
?>
答案 0 :(得分:2)
根据评论中的要求,这是Key方法。
<?
if(!empty($_FILES) && !isset($_POST['Cancel'])) // only process if we haven't clicked Cancel
{
foreach($_FILES as $Key => $File) // loop through the files as we don't know the input file name.
{
if($File['size'] > 0) // check it has a size
{
// do insert, making sure you store $Key with the image
mysql_query("insert into Image (ImageFile, Key) values ('{$File['name']}', '$Key'");
}
}
}
if(isset($_POST['Cancel']) && !empty($_POST['Cancel'])) // if we have clicked cancel
{
$Key = array_shift($_POST['Cancel']); // get the unique key from the post.
mysql_query("delete from Image where Key = '$Key'"); // delete the file from the database.
}
?>
以上是处理上传和取消的PHP代码。
<form action="" method="post" enctype="multipart/form-data">
<? $Key = md5(); ?> <!-- Create the unique key -->
<input type="file" name="<?= $Key; ?>" /> <!-- Set the File input name to the unique key to be accessed via the server. -->
<input type="submit" value="Upload" />
<input type="submit" name="Cancel[<?= $Key; ?>]" value="Cancel" /> <!-- Set the Cancel submit name to include the unique key to be accessed when clicking Cancel -->
</form>
以上是表格。
答案 1 :(得分:0)
在insert语句之后,获取插入的id:mysql_insert_id();
按下取消按钮后,您可以删除插入ID的行
答案 2 :(得分:0)
从this发布后,点击取消按钮更改iframe src会重置发布的数据,即使表单已提交,如果已重置文件上传的表单提交,则应设置$_FILES['fileImage']['error']
一个猜测),我自己没有尝试过,但我想这是一个值得尝试的选择。
否则,如果整个表单只有文件上传字段,则可以将取消按钮设为input type="reset"
,这样可以通过文件上传来中断表单提交,从而阻止插入操作。