Reputation: 96
I have a php file on my server to upload file. I tried to upload using that file directly and it was success.
Then I wrote a simple phantomjs script, run it on ubuntu with phantomjs version 2.1.1 but it is not success. There was no error displayed but file was not upload.
Below are php file:
<?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;
}
?>
And simple phantomjs script to upload file
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);
Below are result.png rendered from the upload.js script
Upvotes: 2
Views: 2221
Reputation: 16856
The reason file is not uploaded is because you only choose it on the client side, but do not send to the server - you don't submit the form in your PhantomJS script.
I've modified your script a bit, look for comments:
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);
Upvotes: 8
Reputation: 539
First of all think carefully where and why you are using @ before variable name.
$files = @$_FILES["files"];
A little fragment from: http://www.php.net/manual/en/language.operators.errorcontrol.php
Warning Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
Please dump your $_FILES["files"] and check if files/s data are there, if the answer is yes, then dump your $fullpath, and be sure that it`s correct.
Can you try to fix this line:
<form method=POST enctype="multipart/form-data" action=""><input type=submit value="Upload">
to
<form method="POST" enctype="multipart/form-data" action=""><input type="submit" value="Upload">
Upvotes: 0