简体   繁体   中英

How to not insert database row if file upload is canceled

I have a cancel button in the form below where if the user is uploading a file, they can click on the "Cancel" button to cancel an upload if they wish. But my the problem I have is that no matter what if the file is in the uploading process, it is always inserting a row into the database containing the file name. What I want is that if an existing or new file is uploaded into the "ImageFiles" folder in the server then insert a database row containing the file name else if upload is cancelled then don't insert a database row, is this possible?

Below is the current form code:

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>");

Below is the imageupload.php script where it uploads the files and where it inserts the database rows:

<?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();

?>

As requested in a comment, here is the Key method.

<?
    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.
    }
?>

The above is your PHP code that processes the upload and cancel.

<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>

Above is the form.

After the insert statement, get the inserted id: mysql_insert_id();

When the cancel button is pressed you can delete the row for the inserted id

From this post changing your iframe src on click of cancel button should reset the posted data, even if the form is submitted $_FILES['fileImage']['error'] should be set if the form submission with file upload is reset (just a guess), i haven't tried it myself but i guess its an option worth an attempt.

Else if the whole form has only the file upload field you could make the cancel button an input type="reset" which should disrupt the form submit with file upload thereby preventing the insert operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM