【发布时间】: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