无法使用PhantomJS上传

时间:2016-03-03 06:33:43

标签: javascript upload phantomjs

我的服务器上有一个php文件来上传文件。我试图直接使用该文件上传,但它很成功。

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

以下是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

result render

2 个答案:

答案 0 :(得分:6)

原因文件未上传是因为您只在客户端选择它,但不发送到服务器 - 您不在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://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);    

答案 1 :(得分:0)

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

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

来自http://www.php.net/manual/en/language.operators.errorcontrol.php

的一小段片段
  

警告   目前,&#34; @&#34; 错误控制运营商前缀甚至会针对将终止脚本执行的关键错误禁用错误报告。除此之外,这意味着如果你使用&#34; @&#34;为了抑制来自某个功能的错误,并且它不可用或者输入错误,脚本将在那里死亡而没有任何关于原因的指示。

请转储您的 $ _ FILES [&#34;文件&#34;] 并检查文件/ s数据是否存在,如果答案为是,则转储 $ fullpath < / strong>,并确保它是正确的。

您可以尝试修复此行:

<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">