0

ここで、ファイルのアップロードをキャンセルしようとしている状況が少し発生しました。私が行ったことは、ユーザーが「キャンセル」ボタンをクリックすると、ファイルをサーバーにアップロードしてデータをデータベースに挿入するページに移動しないように、単に iframe を削除することです。

ユーザーが「キャンセル」ボタンをすぐにクリックした場合、これは問題なく機能しますユーザーが「キャンセル」ボタンをクリックする直前に、ファイルがアップロードされました。

だから私の質問は、ユーザーが「キャンセル」ボタンをクリックする前にファイルが何らかの形でアップロードされた場合、データベース内のデータを削除し、サーバーからファイルを削除する方法はありますか?

以下は画像アップロードフォームです。

<form action="imageupload.php" method="post" enctype="multipart/form-data" target="upload_target_image" onsubmit="return imageClickHandler(this);" class="imageuploadform" >
  <p class="imagef1_upload_process" align="center">
    Loading...<br/>
    <img src="Images/loader.gif" />
  </p>
  <p class="imagef1_upload_form" align="center">
    <br/>
    <span class="imagemsg"></span>
    <label>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">
    <input type="reset" name="imageCancel" class="imageCancel" value="Cancel" />
  </p>
  <iframe class="upload_target_image" name="upload_target_image" src="#" style="width:0px;height:0px;border:0px;solid;#fff;"></iframe>
</form> 

以下は、「キャンセル」ボタンを制御する jquery 関数です。

$(imageuploadform).find(".imageCancel").on("click", function(event) {
    $('.upload_target_image').get(0).contentwindow
    $("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
    return stopImageUpload(2);
});

以下は、ファイルをアップロードしてデータをデータベースに挿入する php コードです。上記のフォームは、この php ページ「imageupload.php」に投稿します。

<body>
<?php

include('connect.php');

session_start();

$result = 0;


//uploads file
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;

//set up the INSERT SQL query command to insert the name of the image file into the "Image" Table
$imagesql = "INSERT INTO Image (ImageFile) 
VALUES (?)";

//prepare the above SQL statement
if (!$insert = $mysqli->prepare($imagesql)) {
  // Handle errors with prepare operation here
}

//bind the parameters (these are the values that will be inserted) 
$insert->bind_param("s",$img);

//Assign the variable of the name of the file uploaded
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];

//execute INSERT query
$insert->execute();

if ($insert->errno) {
  // Handle query error here
}

//close INSERT query
$insert->close();

//Retrieve the ImageId of the last uploded file
$lastID = $mysqli->insert_id; 

//Insert into Image_Question Table (be using last retrieved Image id in order to do this)
$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)  
VALUES (?, ?, ?)"; 

//prepare the above SQL statement
if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
  // Handle errors with prepare operation here 
  echo "Prepare statement err imagequestion"; 
} 

//Retrieve the question number
$qnum = (int)$_POST['numimage'];

//bind the parameters (these are the values that will be inserted) 
$insertimagequestion->bind_param("isi",$lastID, 'Exam', $qnum); 

//execute INSERT query
$insertimagequestion->execute(); 

if ($insertimagequestion->errno) { 
  // Handle query error here 
} 

//close INSERT query
$insertimagequestion->close();

?>

<!--Javascript which will output the message depending on the status of the upload (successful, failed or cancelled)-->

<script>
    window.top.stopImageUpload(<?php echo $result; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
</script>
</body>

アップデート:

以下は、キャンセルされたファイルをサーバーから削除し、データベースからレコードを削除するphpコード「cancelimage.php」です。セットアップは完了していますが、$_SESSION を使用してファイルの名前と ID を取得できるように、誰かがそれを完了できますか?

<?php

// connect to the database
include('connect.php');

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}


//remove file from server
unlink("ImageFiles/...."); //need to retrieve file name here where the ... line is

//DELETE query statement where it will delete cancelled file from both Image and Image Question Table
$imagedeletesql = " DELETE img, img_q 
FROM Image AS img 
LEFT JOIN Image_Question AS img_q 
ON img_q.ImageId = img.ImageId 
WHERE img.ImageFile = ?"; 

//prepare delete query
if (!$delete = $mysqli->prepare($imagedeletesql)) {
// Handle errors with prepare operation here
}

//Dont pass data directly to bind_param store it in a variable
$delete->bind_param("s",$img);

//execute DELETE query
$delete->execute();

if ($delete->errno) {
// Handle query error here
}

//close query
$delete->close();

?>

簡単にするために、回答にサンプルコードを提供してください。ありがとうございました

4

3 に答える 3

1

PDOを使用しているので、次を使用できます:

$insert->rollBack();

最近の挿入をロールバックします。アップロードされたファイルを見つけて使用します。

unlink('test.html');

rollBackとの詳細については、これらのリンクをチェックしてください。unlink

于 2012-10-22T22:01:58.373 に答える
0

が削除され、アップロードされたファイルを通知するコールバックが呼び出されない場合iframe、削除するファイルを知るのは少し難しいでしょう。

最後にアップロードされたファイルの$lastIDとをに保存し、データベースでファイルを検索する他のページを要求することはできますが、レコードを削除してから、ディスクからファイルを削除します。$ImageFile$_SESSION

サンプルコードをいくつか提供することもできますが、現在のコードを修正し、読みやすくしてインデントを修正することをお勧めします。

もう1つの解決策は、次の関数を使用してアップロードを明示的に中止することです。

function abortUpload(){
    var iframeObj = $("iframe[name='upload_target_image']").get(0);
    if (iframeObj.contentWindow.document.execCommand){ 
        // IE
        iframeObj.contentWindow.document.execCommand('Stop');
    }else{
        // other browsers
        iframeObj.contentWindow.stop();
    }
}
于 2012-10-22T22:05:56.907 に答える
0

よし、以前の回答で述べたように、 を使用してと$_SESSIONを格納できます。$lastID$ImageFile

たとえば、次を$lastID = $mysqli->insert_id;挿入した後:

$_SESSION['lastID'] = $lastID;
$_SESSION['ImageFile'] = $_FILES["fileImage"]["name"];

これはセッションに保存lastIDImageFileれます。

次のように入力しcancelimage.phpます。

unlink("ImageFiles/" . $_SESSION['ImageFile']); 

$delete = $mysqli->prepare('DELETE FROM Image WHERE id = ?');
$delete->bind_param("i",$_SESSION['lastID']);
$delete->execute();

次に、iframe のソースを更新するキャンセル ボタンにアクションを追加します。cancelimage.php

ただし、ユーザーが以前にファイルをアップロードしたことがあり、新しいファイルをアップロードしたが、ファイルが通過しなかった (セッション変数が設定されていない) 場合、以前のファイルが削除されるため、リスクがあります。

于 2012-10-22T23:10:05.373 に答える