【问题标题】:Can't upload using PhantomJS无法使用 PhantomJS 上传
【发布时间】:2016-03-03 06:33:43
【问题描述】:

我的服务器上有一个 php 文件来上传文件。我尝试直接使用该文件上传,并且成功。

然后我写了一个简单的 phantomjs 脚本,在 ubuntu 上用 phantomjs 版本 2.1.1 运行它,但没有成功。没有显示错误,但文件没有上传。

以下是php文件:

<?php
if(isset($_FILES["files"]))
{
    $files = $_FILES["files"];
    if($files["name"] != '')
    {

            $fullpath = "./".$files["name"];
            if(move_uploaded_file($files['tmp_name'],$fullpath))
            {                      
                    echo "<h1><a href='$fullpath'>OK-Click here!</a></h1>";        
            }
    }
    echo '<html><head><title>Upload files...</title></head><body><form method=POST enctype="multipart/form-data" action=""><input type="file" name="files"><input type="submit" value="Upload"></form></body></html>';
return;
}
?>

以及上传文件的简单 phantomjs 脚本

var webPage = require('webpage');
var page = webPage.create();
var testindex = 0, loadInProgress = false;


page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

function load()
{
    page.open("http://myserver.com/upload.php?cmd=upload")  
}

function upload()
{
    page.uploadFile('input[name=files]', '/home/user/Desktop/log_no_debug.png');    
    page.render("result.png")
}



var steps = [
    load,
    upload
]

interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] == "function") {
    //console.log("step " + (testindex + 1));
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] != "function") {
    //console.log("test complete!");
    phantom.exit();
  }
}, 50);    

下面是从 upload.js 脚本渲染的 result.png

【问题讨论】:

    标签: javascript upload phantomjs


    【解决方案1】:

    文件未上传的原因是因为您只在客户端选择它,而不是发送到服务器 - 您没有在 PhantomJS 脚本中提交表单。

    我稍微修改了你的脚本,寻找 cmets:

    var webPage = require('webpage');
    var page = webPage.create();
    var testindex = 0, loadInProgress = false;
    
    page.onLoadStarted = function() {
      loadInProgress = true;
      console.log("load started");
    };
    
    page.onLoadFinished = function() {
      loadInProgress = false;
      console.log("load finished");
    };
    
    function load()
    {
        page.open("http://test/upload.php?cmd=upload")  
    }
    
    function upload()
    {
        loadInProgress = true;
        page.uploadFile('input[name=files]', '/path/to/file.ext');    
    
        // Here we submit the form to the server
        page.evaluate(function(){
            // document.querySelectorAll("input[type=submit]")[0].click();
            document.querySelectorAll("form")[0].submit();
        });
    }
    
    // And only after the page has reloaded it is time to make a screenshot
    function finish()
    {
        page.render("result.png");
    }
    
    var steps = [
        load,
        upload,
        finish
    ]
    
    interval = setInterval(function() {
      if (!loadInProgress && typeof steps[testindex] == "function") {
        //console.log("step " + (testindex + 1));
        steps[testindex]();
        testindex++;
      }
      if (typeof steps[testindex] != "function") {
        //console.log("test complete!");
        phantom.exit();
      }
    }, 500);    
    

    【讨论】:

    • 哦,这行得通。我在互联网上担任教程,但从不知道我们需要提交表格。感谢您的帮助
    【解决方案2】:

    首先要仔细考虑在变量名前使用@ 的位置和原因。

     $files = @$_FILES["files"];
    

    来自http://www.php.net/manual/en/language.operators.errorcontrol.php的小片段

    警告 目前,"@" 错误控制运算符前缀甚至会禁用将终止脚本执行的严重错误的错误报告。除其他外,这意味着如果您使用“@”来抑制某个函数的错误,并且该函数不可用或输入错误,则脚本将在那里死掉,而没有说明原因。

    请转储您的 $_FILES["files"] 并检查文件/s 数据是否存在,如果答案是肯定的,则转储您的 $fullpath,然后确保它是正确的。

    你能尝试修复这条线吗:

    <form method=POST enctype="multipart/form-data" action=""><input type=submit value="Upload">
    

    <form method="POST" enctype="multipart/form-data" action=""><input type="submit" value="Upload">
    

    【讨论】:

    • 感谢您的回答,但问题是我可以通过直接浏览该 php 文件来上传文件。但是当我使用phantomjs时它并不成功
    • 哦,我明白了,但是您确定 $_Files["files"] 获取的是正确的文件吗?我的意思是其余的代码接缝都可以。
    • 是的,它正在获取正确的文件,因为我尝试上传了很多次并且它总是没问题。
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 2015-12-09
    • 2014-02-24
    • 2015-07-07
    • 2017-02-08
    • 2016-09-11
    • 1970-01-01
    • 2016-02-13
    相关资源
    最近更新 更多