PHP文件上传完成后不回显

时间:2015-08-16 16:24:49

标签: php

我有一些使用drop zone上传文件的简单代码。它正在上传文件,但由于某种原因,它不会在代码末尾回显“完成上传”。

我错过了一些明显的东西吗?

<script type="text/javascript">
  Dropzone.options.myDropzone = {
    addRemoveLinks: true,
    removedfile: function(file) { 
      var _ref;
      return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
    }
  };
</script>

<div id="dropzone">
    <form id="myDropzone" action="#" class="dropzone" id="demo-upload">
      <div class="dz-message">
        Drop files here or click to upload.<br />
      </div>
    </form>
</div>

<?php
$ds = DIRECTORY_SEPARATOR;  //1
$storeFolder = 'uploads';   //2

if (!empty($_FILES)) {
    $tempFile = $_FILES['file']['tmp_name'];          //3             
    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4
    $targetFile =  $targetPath. $_FILES['file']['name'];  //5

    move_uploaded_file($tempFile,$targetFile); //6
    echo "done uploading";
}
?>  

2 个答案:

答案 0 :(得分:2)

试试这个:

if(move_uploaded_file($tempFile,$targetFile)) {
    echo "done uploading";
} else {
    echo 'error!';
}

您可以激活error_reporting以查看move_uploaded_file

中的错误

编辑:丑陋的黑客,只是为了测试。不要在生产中使用它!

if(move_uploaded_file($tempFile,$targetFile)) {
    $_SESSION["success"] = "upload done";
    echo $_SESSION["success];
} else {
    echo 'error!';
}

答案 1 :(得分:1)

由于Dropzone使用AJAX向服务器发布请求,因此当您回显某些内容时,您将看不到响应行普通的PHP响应。

尝试这种方式

<script type="text/javascript">
    Dropzone.options.myDropzone = {
        ...
        success: function(file, response){
            alert(response); // Just to test, you can remove this
            // Do what you want
            // Like:
            if(response == "success") {
                // Uploaded ok
            } else {
                // Failed to upload
            }
        }
    };
</script>

通过这种方式,在成功的AJAX请求之后,您可以捕获响应并执行您想要的任何操作。

Ofcoruse,就像@tftd说的那样,你需要像move_uploaded_file一样包裹:

if(move_uploaded_file(...)) {
    // done uploading
    echo json_encode('success');
} else {
    // failed moving
    echo json_encode('error');
}