我按照步骤制作会话变量的一步一步表格。
一步我有一个文件上传预览图像并显示一个上传按钮,按下时按钮调用需要修改$ _SESSION变量的php脚本,以及回显上传文件的路径。
当我在没有require_once('config.php')
的调试器中测试它时,它完全按照我的预期工作,显示图像及其文件名,但是,由于某种原因,当我包含配置文件时,我迭代通过它在我的调试器中,它似乎运行PHP脚本两次,它正确更新会话变量,但文件名不再回显,数据在到达前端之前丢失。
我无法判断错误是在config.php
文件中,还是在AJAX调用中,还是在我失踪的其他地方。
标记(' step4.php'):
<form method="post" action="step5.php">
<span id="newfile">
<input type="file" name="uploaded_file" id="imageupload" accept=".jpg, .jpeg, .png">
<img alt="Preview Loading..." id="imagepreview">
</span>
<!-- The submit button is elsewhere before the end of the form, it acts as a next button -->
</form>
javascript函数:
$(document).on('click', '#uploadbutton', function(e) {
e.preventDefault();
//Grab upload elements and and file
var uploadbutton = this;
var uploader = document.getElementById('imageupload');
var file = document.getElementById('imageupload').files[0];
//Hide the upload elements
uploadbutton.parentNode.removeChild(uploadbutton);
uploader.parentNode.removeChild(uploader);
//Make the form and pass it to server
var formData = new FormData();
formData.append('file', file); //$_FILES['file']
$.ajax({
url: 'uploadfile.php',
type: 'POST',
data: formData,
processData: false,
contentType: false
})
.done(function(data) {//Data is the relative path to the file
//Hide the old content
document.getElementById('newfile').innerHTML = '';
//Show the new image
var text = document.createTextNode('Uploaded File: '+data.replace('temp/', '', data));
var br = document.createElement("br");
var imgElement = document.createElement("IMG");
imgElement.setAttribute('src', data);
document.getElementById('newfile').appendChild(text);
document.getElementById('newfile').appendChild(br);
document.getElementById('newfile').appendChild(imgElement);
})
.fail(function() {
alert("Error uploading the file. Hit refresh and try again.");
});
});
&#39; uploadfile.php&#39; :(我的调试器显示的那个被执行两次......)
<?php
//require_once('config.php'); //THE PROBLEM?
if($_FILES) {
//Get the uploaded file information
$name_of_uploaded_file = $_FILES['file']['name'];
//Actually upload the file to the server!
$upload_folder = 'temp/'; //Make a file named temp
$path_of_uploaded_file = $upload_folder.$name_of_uploaded_file;
$tmp_path = $_FILES["file"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
copy($tmp_path,$path_of_uploaded_file);
$_SESSION['step4filepath'] = $path_of_uploaded_file;
echo $path_of_uploaded_file;
}
}
?>
&#39; config.php&#39;:当它被包含时搞砸的东西
<?php
session_start();
//Initialize session data
if(empty($_SESSION)) {
if(!isset($_SESSION['step4filepath'])) {
$_SESSION['step4filepath'] = '';
}
}
//Update session data
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_SESSION['currentPage'] == 4) {
//input for step 4, we just want filename if they submit the form
$_SESSION['step4filepath'] = $_POST['step4filepath'];
}
//Enable us to hit the back button!
header("Location: " . $_SERVER['REQUEST_URI']);
}
?>
完全迷失了。
答案 0 :(得分:0)
可能是这个?为什么配置文件中存在该行?
header("Location: " . $_SERVER['REQUEST_URI']);
答案 1 :(得分:0)
看起来config.php正在用$ _POST ['step4filepath']替换$ _SESSION ['step4filepath']的值,而不是将其保留为$ path_of_uploaded_file。
修改强>
如其他地方所述,header()将导致重新加载。我的答案是无关紧要的,因为在设置var之前调用了config.php。
答案 2 :(得分:0)
解决方案1
替换
$(document).on('click', '#uploadbutton', function(e) {
到
$(document).off('click').on('click', '#uploadbutton', function(e) {
解决方案2:见下面的示例代码
$(document).on("click", "#someID", function(e) {
$(this).attr("disabled","disabled");
// Disabling the input stops the event from firing multiple times.
var targetObj = $(this);
// targetObj can be used within the $.POST function, not $(this)
var myVariable = "Hello world";
$.post("/DoSomethingAJAXY.php", { variable1: myVariable },
function(data, status){
if (status == "success") {
// Do something
}
$(targetObj).removeAttr("disabled");
// Re-enable the event input trigger
});
}