/** * Implementation of NUpload::moveUpload */ public static function moveUpload($src, $target) { $s3 = self::s3_connect(); $target = implode('/', array(self::$prefix, trim($target, '/'))); $file_opts = array('acl' => AmazonS3::ACL_PUBLIC, 'contentType' => NFilesystem::getMimeType($src), 'fileUpload' => $src); $resp = $s3->create_object(self::$bucket, $target, $file_opts); if ($resp->status == 200) { $url = $s3->get_object_url(self::$bucket, $target); self::debug("File uploaded to s3: {$url}"); return $url; } else { throw new NUploadException("Couldn't upload file to s3: {$bucket} - {$filename}", 1); return ''; } }
function putFile($filename) { $s3svc = new S3(); // Removing the first slash is important - otherwise the URL is different. $aws_filename = eregi_replace('^/', '', $filename); $filename = $_SERVER['DOCUMENT_ROOT'] . $filename; $mime_type = NFilesystem::getMimeType($filename); // Read the file into memory. $fh = fopen($filename, 'rb'); $contents = fread($fh, filesize($filename)); fclose($fh); $s3svc->putBucket(MIRROR_S3_BUCKET); $out = $s3svc->putObject($aws_filename, $contents, MIRROR_S3_BUCKET, 'public-read', $mime_type); // Now the file is accessable at: // http://MIRROR_S3_BUCKET.s3.amazonaws.com/put/the/filename/here.jpg OR // http://s3.amazonaws.com/MIRROR_S3_BUCKET/put/the/filename/here.jpg unset($s3svc); }
/** * serveFile - Actually serves the file to the browser. * * @param string A fully qualified filename that needs to be sent to a browser. **/ function serveFile($file_path) { // Find out the mime type of that file. $mime_type = NFilesystem::getMimeType($file_path); // Need the filename for later on. $filename = $this->getFileName($file_path); // Serve the file out like normal. $fp = fopen($file_path, 'rb'); // Send correct headers. header("Content-Type: {$mime_type}"); header("Content-Length: " . filesize($file_path)); header('Content-Disposition: attachment; filename="' . $filename . '"'); fpassthru($fp); }