Home: http://www.mynakedgirlfriend.de/ Version 1.00 (Das Original der Funktion uploadToGallery() hieß PostToHost() und stammt von "Andreas Bohne-Lang / a.bohne@dkfz.de / 19.09.2003 / GPL Quelle: http://www.php-faq.de/q-code-upload.html) */ function uploadToGallery($host, $port, $path, $apiKey, $entity, $image) { $dc = 0; $bo="-----------------------------305242850528394"; $fp = fsockopen($host, $port, $errno, $errstr); if (!$fp) { echo "errno: $errno \n"; echo "errstr: $errstr\n"; return $result; } fputs($fp, "POST $path HTTP/1.1\n"); fputs($fp, "Host: $host\n"); fputs($fp, "X-Gallery-Request-Method: post\n"); fputs($fp, "X-Gallery-Request-Key: " . $apiKey . "\n"); $ds_entity = sprintf("--%s\nContent-Disposition: form-data; name=\"%s\"\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n\n%s\n", $bo, "entity", $entity); $dc += strlen($ds_entity); $ds_image = sprintf("--%s\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\nContent-Type: application/octet-stream\nContent-Transfer-Encoding: binary\n\n%s\n", $bo, "file", $image['filename'], $image['file']); $dc += strlen($ds_image); $dc += strlen($bo)+3; fputs($fp, "Content-length: $dc \n"); fputs($fp, "Content-type: multipart/form-data; boundary=$bo\n\n"); fputs($fp, $ds_entity ); fputs($fp, $ds_image ); $ds = "--".$bo."--\n"; fputs($fp, $ds); $res = ""; while(!feof($fp)) { $res .= fread($fp, 1); } fclose($fp); return $res; } function processFiles($galleryhost, $galleryport, $itemId, $apiKey, $importdir, $archivedir = NULL) { if ($handle = opendir($importdir)) { echo "Processing \"" . $importdir . "\"...\n"; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $entity = '{"name":"' . $file . '","type":"photo"}'; $filehandle = fopen($importdir . "/" . $file,"r"); $image['file'] = fread($filehandle, filesize($importdir . "/" . $file)); $image['filename'] = $file; $response = uploadToGallery($galleryhost, $galleryport, "/index.php/rest/item/" . $itemId, $apiKey, $entity, $image); //echo "\n\n-------\n" . $response . "\n--------\n\n"; $success = FALSE; if(preg_match("/HTTP\/1\.1 201 Created/", $response)) { $success = TRUE; if($archivedir != NULL) { $success = rename($importdir . "/" . $file, $archivedir . "/" . $file); } } if($success) { echo "\tDatei \"" . $importdir . "/" . $file . "\" OK.\n"; }else { echo "\tFehler bei Datei \"" . $importdir . "/" . $file . "\".\n"; } } } closedir($handle); } } $galleryHost = "gallery3.site"; $galleryPort = 80; $apiKey = "xxx"; $itemID = 1; $importDir = "/data/upload"; $archiveDir = "/data/archive"; processFiles($galleryHost, $galleryPort, $itemID, $apiKey, $importDir, $archiveDir); ?>