我在下面的表格中有一个取消按钮,如果用户正在上传文件,他们可以根据需要单击"取消"按钮取消上传。但我遇到的问题是,无论文件是否在上传过程中,它总是在包含文件名的数据库中插入一行。我想要的是,如果将现有文件或新文件上传到服务器的"ImageFiles"文件夹中,然后插入包含文件名的数据库行,否则如果上传被取消,则不要插入数据库行,这可能吗?
以下是当前的表单代码:
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();
?>
根据评论中的要求,这里是 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>
以上是表格。
在插入语句之后,获取插入的 id: mysql_insert_id();
按下取消按钮后,您可以删除插入 id 的行
从这篇文章中,单击取消按钮更改iframe src应该重置发布的数据,即使表单已$_FILES['fileImage']['error']
提交,如果重置带有文件上传的表单提交,也应该设置表单上传(只是一个猜测(,我自己还没有尝试过,但我想这是一个值得尝试的选项。
否则,如果整个表单只有文件上传字段,则可以将取消按钮设为input type="reset"
,这应该会中断表单提交和文件上传,从而阻止插入操作。