-1

可能重复:
文件在被取消之前上传

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

取消音频.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']并且用户上传然后取消文件,我怎样才能阻止它将文件插入服务器并插入数据库记录?

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

音频上传.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 源更改为“canceaudio.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;
}
4

1 回答 1

0

为了摆脱你未定义的索引的东西,加上让你的脚本更加健壮,你应该添加错误检查。哪怕是一个简单的

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

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

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

于 2012-10-24T17:10:04.733 回答