0

我在下面的表单中有一个取消按钮,如果用户正在上传文件,他们可以单击“取消”按钮取消上传,如果他们愿意的话。但是我遇到的问题是,无论文件是否处于上传过程中,它总是在包含文件名的数据库中插入一行。我想要的是,如果将现有文件或新文件上传到服务器中的“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();

?>
4

3 回答 3

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>

以上是表格。

于 2012-05-18T08:00:13.307 回答
0

在插入语句之后,获取插入的 id:mysql_insert_id();

按下取消按钮时,您可以删除插入 id 的行

于 2012-05-18T07:27:15.430 回答
0

这篇文章中,单击取消按钮更改您的 iframe src 应该重置发布的数据,即使表单提交$_FILES['fileImage']['error']也应该设置如果带有文件上传的表单提交被重置(只是一个猜测),我自己没有尝试过但是我想这是一个值得尝试的选择。

否则,如果整个表单只有文件上传字段,您可以制作取消按钮,input type="reset"这应该会通过文件上传中断表单提交,从而阻止插入操作。

于 2012-05-18T07:39:50.860 回答