如何停止将文件插入服务器

时间:2012-10-24 16:57:06

标签: php pdo mysqli

  

可能重复:
  files get uploaded just before they get cancelled

我有取消文件上传的情况。假设发生的是,如果用户点击“取消”按钮,它将转到下面的脚本并从服务器中删除该文件并从数据库中删除文件的记录:

cancelaudio.php

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

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

  unlink("AudioFiles/" . $_SESSION['AudioFile']);  //undefined notice

$delete = $mysqli->prepare('DELETE FROM Audio WHERE AudioId = ?'); 
$delete->bind_param("i",$_SESSION['lastAudioID']); 
$delete->execute(); 

$deleteaud = $mysqli->prepare('DELETE FROM Audio_Question WHERE AudioId = ?'); 
$deleteaud->bind_param("i",$_SESSION['lastAudioID']); 
$deleteaud->execute(); 


?>

现在情况就是这样:

如果我清理我的cookie,显然我没有存储我的$ _SESSION变量的文件。现在,如果我尝试上传文件(第一个文件)但是取消它,那么我得到一个未定义的“AudioFile”索引通知。这很公平,因为很明显我没有存储在$ _SESSION变量中的文件。但问题是它没有在上面的代码中执行unlink()和DELETE语句,因此它将显示文件的记录并仍然上传它。

所以发生的事情是,直到我在服务器中有一个文件和存储在$ _SESSION ['AudioFile']中的文件,它不会从服务器中删除该文件,并且它将从数据库中删除该记录

所以我的问题是我需要做什么,如果$_SESSION['AudioFile']中没有文件并且用户上传然后取消文件,我怎么能阻止它将文件插入到服务器并插入数据库记录?

下面是上传文件并将数据插入数据库的代码:

audioupload.php

<?php

session_start();

ini_set('display_errors',1); 
error_reporting(E_ALL);
// connect to the database
include('connect.php');

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

$result = 0;

if( file_exists("AudioFiles/".$_FILES['fileAudio']['name'])) {
$parts = explode(".",$_FILES['fileAudio']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;

while( file_exists("AudioFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileAudio']['name'] = $base."_".$n.".".$ext;

move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
"AudioFiles/" . $_FILES["fileAudio"]["name"]);
$result = 1;

}
else
{
move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
"AudioFiles/" . $_FILES["fileAudio"]["name"]);
$result = 1;
}

$audiosql = "INSERT INTO Audio (AudioFile) 
VALUES (?)";

if (!$insert = $mysqli->prepare($audiosql)) {
// Handle errors with prepare operation here
}

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

//Assign the variable
$aud = 'AudioFiles/'.$_FILES['fileAudio']['name'];

$insert->execute();

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

$insert->close();

$lastAudioID = $mysqli->insert_id;   

$_SESSION['lastAudioID'] = $lastAudioID; 
$_SESSION['AudioFile'] = $_FILES["fileAudio"]["name"]; 


$audioquestionsql = "INSERT INTO Audio_Question (AudioId, QuestionId)  
VALUES (?, ?)"; 

if (!$insertaudioquestion = $mysqli->prepare($audioquestionsql)) { 
// Handle errors with prepare operation here 
echo "Prepare statement err audioquestion"; 
} 

$qnum = (int)$_POST['numaudio'];

$insertaudioquestion->bind_param("iii",$lastAudioID, $qnum); 

$insertaudioquestion->execute(); 

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

$insertaudioquestion->close(); 



?>

以下是文件输入的HTML表单:

<form action='audioupload.php' method='post' enctype='multipart/form-data' target='upload_target_audio' onsubmit='return audioClickHandler(this);' class='audiouploadform' > 

Audio File: <input name='fileAudio' type='file' class='fileAudio' /></label><br/><br/><label class='audiolbl'> 

<input type='submit' name='submitAudioBtn' class='sbtnaudio' value='Upload' /></label>

<input type='hidden' class='numaudio' name='numaudio' value='" + GetFormAudioCount() + "' />

<label><input type='reset' name='audioCancel' class='audioCancel' value='Cancel' /></label>

<iframe class='upload_target_audio' name='upload_target_audio' src='#' style='width:300px;height:300px;border:0px;solid;#fff;'></iframe></form>

下面是jquery函数,如果用户在文件上传时单击“取消”按钮,它只会将iframe源更改为“cancelaudio.php”:

function startAudioUpload(audiouploadform){
$(audiouploadform).find('.audiof1_upload_process').css('visibility','visible');
$(audiouploadform).find('.audiof1_upload_form').css('visibility','hidden');
sourceAudioForm = audiouploadform;

$(audiouploadform).find(".audioCancel").on("click", function(event) {
$('.upload_target_audio').get(0).contentwindow
$("iframe[name='upload_target_audio']").attr("src", "cancelaudio.php");
return stopAudioUpload();
});

return true;
}

1 个答案:

答案 0 :(得分:0)

要摆脱未定义的索引,加上使脚本更加健壮,您应该添加错误检查。即使是简单的

if ($_FILES['fileAudio']['error'] === UPLOAD_ERR_OK) {
   ... successful upload ...
}

会减少你的很多问题。现在,您的代码只是假设上传成功,甚至无需检查上传是否是尝试过的事件。

此处定义了各种失败的错误代码:http://php.net/manual/en/features.file-upload.errors.php