无法上传使用PhantomJS
问题描述:
我有一个php文件在我的服务器上传文件。我试图直接使用该文件进行上传,并取得成功。无法上传使用PhantomJS
然后我写了一个简单的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脚本
答
原因文件没有上传,因为你只能选择它的客户端,但不发送到服务器 - 您不要在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);
答
被result.png呈现
首先仔细想想你在哪里和为什么使用@ be前面的变量名称。
$files = @$_FILES["files"];
一个小片段来自:http://www.php.net/manual/en/language.operators.errorcontrol.php
警告 目前“@”错误控制运算符前缀甚至禁用错误的,将终止脚本执行关键错误报告。除此之外,这意味着如果您使用“@”来抑制某个函数的错误,并且它不可用或错误输入,那么脚本就会在那里死去,而不会显示原因。
请转储$ _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">
哦,现在很好赶:) :) – fsn
哦,这工作。我在互联网上做过教程,但从来不知道我们需要提交表单。谢谢你的帮助 – Rain