Browse Source

Implemented file uploads (multipart), see #86

Maurits van der Schee 8 years ago
parent
commit
0cd5991fc9
3 changed files with 44 additions and 7 deletions
  1. 24
    0
      README.md
  2. 20
    5
      api.php
  3. 0
    2
      tests/tests.php

+ 24
- 0
README.md View File

@@ -38,6 +38,7 @@ This is a single file application! Upload "api.php" somewhere and enjoy!
38 38
   - Supports POST variables as input
39 39
   - Supports a JSON object as input
40 40
   - Supports a JSON array as input (batch insert)
41
+  - Supports file upload from web forms (multipart/form-data)
41 42
   - Condensed JSON ouput: first row contains field names
42 43
   - Sanitize and validate input using callbacks
43 44
   - Permission system for databases, tables, columns and records
@@ -583,6 +584,29 @@ icon=ZGF0YQ
583 584
 
584 585
 In the above example you see how binary data is sent. Both "base64url" and standard "base64" are allowed (see rfc4648).
585 586
 
587
+## File uploads
588
+
589
+You can also upload a file using a web form (multipart/form-data) like this:
590
+
591
+```
592
+    <form method="post" action="http://localhost/api.php/icons" enctype="multipart/form-data">
593
+        Select image to upload:
594
+        <input type="file" name="icon">
595
+        <input type="submit">
596
+    </form>
597
+```
598
+
599
+Then this is handled as if you would have sent:
600
+
601
+```
602
+POST http://localhost/api.php/icons
603
+{"icon_name":"1px.gif","icon_type":"image\/gif","icon":"R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=","icon_error":0,"icon_size":32}
604
+```
605
+
606
+As you can see the "xxx_name", "xxx_type", "xxx_error" and "xxx_size" meta fields are added (where "xxx" is the name of the file field).
607
+
608
+NB: You cannot edit a file using this method, because browsers do not support the "PUT" method in these forms.
609
+
586 610
 ## Spatial/GIS support
587 611
 
588 612
 There is also support for spatial filters:

+ 20
- 5
api.php View File

@@ -1351,9 +1351,8 @@ class PHP_CRUD_API {
1351 1351
 		return array($tableset,$collect,$select);
1352 1352
 	}
1353 1353
 
1354
-	protected function retrieveInput($post) {
1354
+	protected function retrieveInputs($data) {
1355 1355
 		$input = (object)array();
1356
-		$data = trim(file_get_contents($post));
1357 1356
 		if (strlen($data)>0) {
1358 1357
 			if ($data[0]=='{' || $data[0]=='[') {
1359 1358
 				$input = json_decode($data);
@@ -1491,9 +1490,9 @@ class PHP_CRUD_API {
1491 1490
 		if ($tenancy_function) $this->applyTenancyFunction($tenancy_function,$action,$database,$fields,$filters);
1492 1491
 		if ($column_authorizer) $this->applyColumnAuthorizer($column_authorizer,$action,$database,$fields);
1493 1492
 
1494
-		if ($post) {
1493
+		if (strlen($post)) {
1495 1494
 			// input
1496
-			$contexts = $this->retrieveInput($post);
1495
+			$contexts = $this->retrieveInputs($post);
1497 1496
 			$inputs = array();
1498 1497
 			foreach ($contexts as $context) {
1499 1498
 				$input = $this->filterInputByFields($context,$fields[$tables[0]]);
@@ -1697,6 +1696,22 @@ class PHP_CRUD_API {
1697 1696
 		$this->endOutput($callback);
1698 1697
 	}
1699 1698
 
1699
+	protected function retrievePostData() {
1700
+		if ($_FILES) {
1701
+			$files = array();
1702
+			foreach ($_FILES as $name => $file) {
1703
+				foreach ($file as $key => $value) {
1704
+					switch ($key) {
1705
+						case 'tmp_name': $files[$name] = base64_encode(file_get_contents($value)); break;
1706
+						default: $files[$name.'_'.$key] = $value;
1707
+					}
1708
+				}
1709
+			}
1710
+			return http_build_query(array_merge($files,$_POST));
1711
+		}
1712
+		return file_get_contents('php://input');
1713
+	}
1714
+
1700 1715
 	public function __construct($config) {
1701 1716
 		extract($config);
1702 1717
 
@@ -1742,7 +1757,7 @@ class PHP_CRUD_API {
1742 1757
 			$get = $_GET;
1743 1758
 		}
1744 1759
 		if (!$post) {
1745
-			$post = 'php://input';
1760
+			$post = $this->retrievePostData();
1746 1761
 		}
1747 1762
 
1748 1763
 		// connect

+ 0
- 2
tests/tests.php View File

@@ -21,8 +21,6 @@ class API
21 21
 		$query = isset($url['query'])?$url['query']:'';
22 22
 		parse_str($query,$get);
23 23
 
24
-		$data = 'data://text/plain;base64,'.base64_encode($data);
25
-
26 24
 		$this->api = new PHP_CRUD_API(array(
27 25
 				'dbengine'=>PHP_CRUD_API_Config::$dbengine,
28 26
 				'hostname'=>PHP_CRUD_API_Config::$hostname,

Loading…
Cancel
Save