Jump to content

Upload file via API


felix21

Recommended Posts

Posted

Im trying to upload files via the REST Api but the file gets corrupt.

Code:

$handle = fopen("/home/test/test.pdf", "r");
$contents = fread($handle, filesize("/home/test/test.pdf"));
fclose($handle);

$post = array(
	'category' => 4,
	'author' => 2,
	'title' => 'Test',
	'description' => 'Test',
	'files' => array('test.pdf' => $contents),
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);

Any suggestions?

Posted
$post = array(
	'category' => 1,
	'author' => 1,
	'title' => 'Test title',
	'description' => 'test description',
	'files' => array(
		"test.txt" => file_get_contents('/home/test/test.txt')
	)
);

This should be correct

Posted
30 minutes ago, BomAle said:

$post = array(
	'category' => 1,
	'author' => 1,
	'title' => 'Test title',
	'description' => 'test description',
	'files' => array(
		"test.txt" => file_get_contents('/home/test/test.txt')
	)
);

This should be correct

It works but the file gets corrupt?

Posted

I tried an html file with tags and some symbols and it don't get corrupt.

var_dump says:

string(2299) "{
   "id": null,
   "title": "Tes3t",
   "category": {
       "id": 4,
       "name": "test",
       "url": "https:\/\/example.com\/files\/category\/4-test\/"
   },
   "author": {
       "id": 2,
       "name": "test",
       "title": null,
       "timeZone": "Europe\/Stockholm",
       "formattedName": "test",
       "primaryGroup": {
           "id": 3,
           "name": "Medlem",
           "formattedName": "Medlem"
       },
       "secondaryGroups": [],
       "email": "test@example.com",
       "joined": "2017-05-06T01:07:33Z",
       "registrationIpAddress": "000.000.000.000",
       "warningPoints": 0,
       "reputationPoints": 0,
       "photoUrl": "https:\/\/example.com\/uploads\/set_resources_1\/84c1e40ea0e759e3f1505eb1788ddf3c_default_photo.png",
       "profileUrl": "https:\/\/example.com\/profile\/2-test\/",
       "validating": false,
       "posts": 17,
       "lastActivity": "2017-05-06T01:33:42Z",
       "lastVisit": "1970-01-01T00:00:00Z",
       "lastPost": "2017-05-06T18:57:33Z",
       "profileViews": 0,
       "birthday": null,
       "customFields": {
           "1": {
               "name": "Personal Information",
               "fields": {
                   "1": {
                       "name": "About Me",
                       "value": null
                   }
               }
           },
           "2": {
               "name": "Converted",
               "fields": {
                   "2": {
                       "name": "Facebook Profile",
                       "value": null
                   }
               }
           }
       }
   },
   "date": "2017-05-06T18:57:33Z",
   "description": "Test3",
   "version": null,
   "changelog": null,
   "files": [
       {
           "name": "test.html",
           "url": "https:\/\/example.com\/uploads\/monthly_2017_05\/test_html.bae1b898740569d1e4f840aae5e1e1ed",
           "size": 161
       }
   ],
   "screenshots": [],
   "primaryScreenshot": null,
   "downloads": null,
   "comments": 0,
   "reviews": 0,
   "views": 0,
   "prefix": null,
   "tags": [],
   "locked": false,
   "hidden": true,
   "pinned": false,
   "featured": false,
   "url": "https:\/\/example.com\/files\/file\/29-tes3t\/"
}"

However if I try with jpg or pdf file it does get corrupt

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...