dougua3705 2017-04-11 05:52
浏览 72


I'm making a step by step form that stores session variables as you follow the steps.

On one step I have a file upload that previews the image and shows an upload button, which when pressed calls a php script that needs to modify a $_SESSION variable, as well as echo the path to the uploaded file.

When I test it in my debugger without require_once('config.php'), it works exactly as I anticipate, showing the image and it's filename, however, for some reason when I include the config file, when I iterate through it in my debugger, it appears to run the php script twice, it correctly updates the session variable, but the filename isn't echoed anymore and the data is lost before it reaches the frontend.

I can't tell if the mistake is in the config.php file, or in the AJAX call, or maybe somewhere else where I'm missing.

The markup ('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">
    <!-- The submit button is elsewhere before the end of the form, it acts as a next button -->

The javascript function:

$(document).on('click', '#uploadbutton', function(e) {
    //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
    //Make the form and pass it to server
    var formData = new FormData();
    formData.append('file', file); //$_FILES['file']
        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);
    .fail(function() {
        alert("Error uploading the file. Hit refresh and try again.");

'uploadfile.php': (the one that my debugger shows gets executed twice...)

//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)) {
        $_SESSION['step4filepath'] = $path_of_uploaded_file;
        echo $path_of_uploaded_file;


'config.php': the one that screws stuff up when it's included


//Initialize session data
if(empty($_SESSION)) {
    if(!isset($_SESSION['step4filepath'])) {
        $_SESSION['step4filepath'] = '';

//Update session data

    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']);

Totally lost on this.

  • 写回答

3条回答 默认 最新

  • dtv995719 2017-04-11 05:55

    May be this? Why that line is there on your config file?

    header("Location: " . $_SERVER['REQUEST_URI']);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT
  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面