Created
May 2, 2013 01:02
-
-
Save JasonMortonNZ/5499511 to your computer and use it in GitHub Desktop.
Laravel S3 image uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Routes.php | |
Route::get('upload', function() | |
{ | |
return View::make('upload-form'); | |
}); | |
Route::post('upload', function() | |
{ | |
// Get and move uploaded file. | |
Input::file('image')->move("/tmp", "test-image.jpg"); | |
// Register AWS instance | |
$s3 = App::make('aws')->get('s3'); | |
$s3->putObject(array( | |
'Bucket' => 's3.trial', | |
'Key' => 'test-image', | |
'SourceFile' => '/tmp/test-image.jpg' | |
)); | |
// Delete local file | |
File::delete("/tmp/test-image.jpg"); | |
return 'Image uploaded to S3' . " - " . '<a href="/view-image">Click here to view image</a>'; | |
}); | |
Route::get('view-image', function() | |
{ | |
$img = "test-image"; // Would be something like $user->img_url; | |
$url = "https://s3-us-west-2.amazonaws.com/s3.trial/" . $img; // Where s3.trial is your bucket name | |
return '<img src="' . $url . '">'; | |
}); | |
-------------------------- | |
upload-form.blade.php | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>S3 - Upload Form</title> | |
<link rel="stylesheet" href="/css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<dov class="row-fluid"> | |
<span12> | |
<form action="upload" method="post" enctype="multipart/form-data"> | |
<fieldset> | |
<legend>Upload Image form</legend> | |
<label for="inputImage">Image:</label> | |
<input type="file" name="image" id="inputImage"> | |
<input type="submit" value="upload" class="btn"> | |
</fieldset> | |
</form> | |
</span12> | |
</dov> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment