0

What I'm trying to do is save the binary file sent through CURL from another server.

The php uploader I have is:

<?php
define("UPLOAD_DIR", "/tmp/");

if (!empty($_FILES["myFile"])) {
    $myFile = $_FILES["myFile"];

    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // ensure a safe filename
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    // don't overwrite an existing file
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }

    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"],
        UPLOAD_DIR . $name);
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }

    // set proper permissions on the new file
    chmod(UPLOAD_DIR . $name, 0644);
}

So i have this on my site as such:

example.com/uploader.php

On server number 2, I run the command:

curl --request POST  --data-binary "@binary.jpg"  http://example.com/uploader.php

and then I check my /tmp/ folder for the file named binary.jpg but it is not there, what is the issue here ?

Thanks.

bobb
  • 9
  • 1

2 Answers2

0

It may at location where you run curl command. For example if you run at /home/student then binary. Jpg is at the location /home/student/binary. Jpg After running curl command, are you getting successful transfer message?

user3464093
  • 115
  • 1
  • 6
0

In the code, if (!empty($_FILES["myFile"])) { , you are specifically checking field named myFile of array $_FILES and then doing stuff. While the curl command does not suggest that the binary file is being sent with name myFile. You can sepcify the name of the POST fields in you curl command and issue will be resolved hopefully. (Also try printing the whole $_POST and $_FILES array in code for debugging purposes)

Cashif Ilyas
  • 1,619
  • 2
  • 14
  • 22
  • How exactly would that be ? Like this? curl --request POST --data-binary "myFile=@binary.jpg" http://example.com/uploader.php – bobb Jun 22 '16 at 06:36
  • I did curl --request POST --data-binary myFile@binary.jpg http://example.com/uploader.php , no result. This is so frustrating, it must be so simple ? – bobb Jun 22 '16 at 06:39