最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Can't upload using PhantomJS - Stack Overflow

matteradmin6PV0评论

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(".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 plete!");
    phantom.exit();
  }
}, 50);    

Below are result.png rendered from the upload.js script

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./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 plete!");
    phantom.exit();
  }
}, 50);    

Below are result.png rendered from the upload.js script

Share Improve this question edited Mar 3, 2016 at 19:40 Artjom B. 62k26 gold badges136 silver badges231 bronze badges asked Mar 3, 2016 at 6:33 RainRain 9611 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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 ments:

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 plete!");
    phantom.exit();
  }
}, 500);    

First of all think carefully where and why you are using @ before variable name.

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

A little fragment from: http://www.php/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">
Post a comment

comment list (0)

  1. No comments so far