Jun 07
cURL is a great library. It can do just about anything that a normal web browser can do including send a file via a post request.
This makes it really easy to transmit files between computers. In my case, I was looking for an easy way to send images snapped by various webcam systems to a central server with php managing the images.
Here is a simple script to send a file with php/cURL via POST:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <?php
$target_url = 'http://127.0.0.1/accept.php';
//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('./sample.jpeg');
/* curl will accept an array here too.
* Many examples I found showed a url-encoded string instead.
* Take note that the 'key' in the array will be the key that shows up in the
* $_FILES array of the accept script. and the at sign '@' is required before the
* file name.
*/
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?> |
And here is the corresponding script to accept the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";
?> |
And that’s it.
Navigate to the ‘send’ script and it will transmit the file sample.jpeg to the accept script.
Note that you can include other arguments in the post along with the file. This allows you to authenticate the upload. I’m using pre-shared strings to ‘validate’ that upload came from my send script.
This works with the command line version of php too.
References:
http://us3.php.net/manual/en/function.move-uploaded-file.php
http://us3.php.net/manual/en/features.file-upload.post-method.php
http://curl.haxx.se/libcurl/php/examples/multipartpost.html
http://forums.devshed.com/php-development-5/php-curl-send-a-file-533233.html
Tagged with: cURL • PHP • POST
Apr 26
Using brian’s scripts a base and google’s jdo documentation, I’ve got a working example of reading from the app engine data store with php.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php
import phptest.test;
import phptest.PMF;
$p = PMF::get();
$pm = $p->getPersistenceManager();
$q = $pm->newQuery("select from phptest.test");
$results = $q->execute();
//Returns a php array of resources. Foreach doesn't seem to work here. This seems to be an issue with Quercus.
$account = count($results);
for($a=0;$a < $account; $a++){
echo $results[$a]->getId() . " - - " . $results[$a]->getFirstName() . '<br/>';
}
$pm->close();
?> |
Its just that easy.
Deletes work the same way too. The JDO persistence manager has a method called ‘deletePersistentAll()’ which will truncate the table. To delete a specific item, select it and call ‘deletePersistent(obj)’ where the obj is your record object. See the comments below.
JDO updates your data automatically. Select the item. Change the data and when you call the persistence managers close method, the changes will be saved.
Need more info: Read the google docs.
Tagged with: app engine • PHP • quercus
Apr 26
Google recently expanded their app engine service to allow java in addition to python. No php or ruby support yet. Choosing java opens the door for both.
Quercus is a pure java implementation of php and it allows us to run php code in app engine. Some really smart people figured out how to make it work. Check out brian and webdigi.
Using their pre-configured environments its actually pretty easy. Brian even figured out how to start saving stuff into the app engine data store. Good stuff.
If your interesting in playing with this, here are some links you’ll need in addition to the ones provided by Brian and friends:
Google’s docs for app engine data connector -Explains how jdo works. I’m a php guy, not a java guy. Using Brian’s example on how to write to and the docs found here, you should be able to get started reading and writing to your accounts datastore.
App engine SDK – Get the eclipse plug-in. Its got a local development server included so you can test your apps before sending them up to google. It also has a push button upload. The module can also co-exist with Zend’s PDT plug-in.
NOTE: When starting the development server, you may get an error from java the first couple of times the page loads. Just reload the page a couple of times and the error will go away.
Recent Comments