1

So I've been trying to upload a file via Curl. I have a txt file on my local server in the root directory. When I try it, it says that the file was uploaded successfully but, the text inside the txt isn't the same as the txt file I have.

    $postData = array(
        'folderId' => '3276800',
        'filename' => 'TxT_12323.txt',
        'filedata' => '@847327489732984.txt',
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.1.103/services/rest/document/upload");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_USERPWD, "admin" . ":" . "admin");

    $result = curl_exec($ch);

    if (curl_errno($ch))
    {
        echo 'Error:' . curl_error($ch);
        exit();
    }

    echo "Upload Successful!<br><br>Return: " . $result;

    curl_close($ch);

File that is uploaded is suppose to have the same inner text as 847327489732984.txt but, when opening the .txt it just has "@847327489732984.txt" inside it.

So, it's not uploading the file correctly.

I fixed this by instead of

'filedata' => '@847327489732984.txt'

I did

'filedata' => file_get_contents('123.pdf')
4
  • Why would it have this suppose to have the same inner text as 847327489732984.txt . I suggest reading the file into a variable and sending that as the data. If this is handled on the server the upload is sent to, then the code there must be checked. Commented Jan 2, 2019 at 22:21
  • @ArtisticPhoenix Problem with that is after I get this working I am gonna upload PDFs and you can't really do that with PDFs. Commented Jan 2, 2019 at 22:24
  • you can't really do that with PDFs why not? Files are just string data, doesn't matter the type of file. Commented Jan 2, 2019 at 22:25
  • Oh.. You right.. It worked just fine. Commented Jan 2, 2019 at 22:31

1 Answer 1

2

You should do something like this

$postData = array(
    'folderId' => '3276800',
    'filename' => 'TxT_12323.txt',
    'filedata' => file_get_contents('847327489732984.txt'),
);

For the most part you can just treat any file as string data in PHP. That doesn't mean you can read and make sense of them, but they can be represented that way in a variable or output and should work just fine.

2
  • I just figured out and was updating the question as you posted this. Commented Jan 2, 2019 at 22:34
  • Sure no problem. Commented Jan 2, 2019 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.