Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:00:17+00:00 2026-06-04T03:00:17+00:00

I have a cancel button in the form below where if the user is

  • 0

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

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:00:19+00:00Added an answer on June 4, 2026 at 3:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cancel button where the user can cancel on a file upload
I have a Cancel button which appears when the user is uploading a file.
In the dialog form below, I have 2 buttons: Save & Cancel. When user
I have a cancel button on a page. But this page can be opened
I have a problem when the user has clicked on the Cancel button. When
I have a cancel button in a form: @using (Html.BeginForm(ConfirmBid,Auction)) { some stuff ...
I have a form, this form has a submit and a cancel button. when
I have a Cancel button where when the user clicks on it, it resets
I have a form with two buttons, a submit button and a cancel/close button.
I have been trying to figure out how to cancel a file upload to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.