{"id":716,"date":"2016-07-11T21:38:42","date_gmt":"2016-07-11T16:08:42","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=716"},"modified":"2026-09-12T12:20:34","modified_gmt":"2026-09-12T06:50:34","slug":"php-scriptweburl-to-upload-file-to-s3","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/php-scriptweburl-to-upload-file-to-s3\/","title":{"rendered":"How to Upload Files to an Amazon S3 Bucket Using PHP and AWS SDK"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Amazon S3 is a scalable object storage service that can be used to store and manage files securely. This guide explains how to create a simple PHP-based file upload application that uploads a file to an Amazon S3 bucket using the AWS SDK for PHP. The script first stores the uploaded file temporarily on the server and then transfers it to the specified S3 bucket.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, make sure you have:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A web server capable of running PHP applications.<\/li>\n\n\n\n<li>Composer installed on the server.<\/li>\n\n\n\n<li>AWS access credentials with appropriate permissions to upload objects to the S3 bucket.<\/li>\n\n\n\n<li>An existing S3 bucket and the required AWS Region.<\/li>\n\n\n\n<li>The uploaded file size must be less than <strong>1 MB<\/strong>.<\/li>\n\n\n\n<li>The PHP application must have permission to create and write to the temporary upload directory.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n<h2>Implementation<\/h2>\n<p><strong>Script Setup:<\/strong><\/p>\n<p>Install aws-sdk in the domain document Root<\/p>\n<pre>composer require aws\/aws-sdk-php<\/pre>\n<p>If Composer is not installed, use the following command to install Composer.<\/p>\n<pre>curl -sS https:\/\/getcomposer.org\/installer | php<\/pre>\n<p>Copy the following script and upload it to the document root directory. Replace all the bolded lines with Appropriate values<br \/><code>&lt;!-- Php<\/code><\/p>\n<p><code> File to upload image to s3 Bucket<\/code><br \/><code>Author: Dhanasekaran N<\/code><br \/><code>Email:support@pheonixsolutions.com<\/code><br \/><code>Next time even Bigger with more. Love to do it in PHP<\/code><br \/><code>Version:0.1<\/code><br \/><code>--&gt;<\/code><br \/><code>&lt;?php<\/code><br \/><code>require 'vendor\/autoload.php';<\/code><br \/><code> use Aws\\S3\\S3Client;<\/code><br \/><code> use Aws\\S3\\Exception\\S3Exception;<\/code><br \/><code> ini_set('display_errors', 1);<\/code><br \/><code> if(isset($_POST['upload']))<\/code><br \/><code> {<\/code><br \/><code> $target_dir=\"\/tmp\/s3upload\/\";<\/code><br \/><code> $mode=0777;<\/code><br \/><code> echo \"Worray. Its working.....\";<\/code><br \/><code> $filename=$_FILES['s3file']['name'];<\/code><br \/><code> $filesize=$_FILES['s3file']['size'];<\/code><br \/><code> echo $fileextension=pathinfo($filename, PATHINFO_EXTENSION);<\/code><br \/><code> echo $target_file=$target_dir.$filename;<\/code><br \/><code> if(strlen($filename) &gt; 0)<\/code><br \/><code> {<\/code><br \/><code> if($filesize&lt;(1024*1024))<\/code><br \/><code> {<\/code><br \/><code> #echo \"File is less than 1MB. Let's Upload...\";<\/code><br \/><code> if(!is_dir($target_dir))<\/code><br \/><code> {<\/code><br \/><code> mkdir($target_dir,$mode, true);<\/code><br \/><code> }<\/code><br \/><code> \/\/move_uploaded_file($_FILES['s3file']['name'], $target_file);<\/code><br \/><code> if(move_uploaded_file($_FILES['s3file']['tmp_name'], $target_file))<\/code><br \/><code> {<\/code><br \/><code> #echo \"Local Uploaded Successfully\";<\/code><br \/><code> echo $target_file;<\/code><br \/><code> echo \"Lets put the object to s3...\";<\/code><br \/><code><code> $bucket=\"<\/code><\/code><strong>&lt;BUCKET-NAME&gt;<\/strong><code>\";<\/code><br \/><code> $client = new Aws\\S3\\S3Client([<\/code><br \/><code> 'version' =&gt; 'latest',<\/code><br \/><code><code> 'region' =&gt; '<\/code><\/code><strong>&lt;Region&gt;<\/strong><code>',<\/code><br \/><code> 'credentials' =&gt; [<\/code><br \/><code><code><\/code><\/code><strong> &#8216;key&#8217; =&gt; &#8216;XXXXXX&#8217;<\/strong><code>, # &lt;- Put your Aws Access Key<\/code><br \/><code><code><\/code><\/code><strong> &#8216;secret&#8217; =&gt; &#8216;xxxxxxx<\/strong><code>' #&lt;- Put your Secret Key<\/code><br \/><code> ]<\/code><br \/><code> ]);<\/code><br \/><code> try<\/code><br \/><code> {<\/code><br \/><code> echo \"Trying upload to s3\";<\/code><br \/><code> $client-&gt;putObject(array(<\/code><br \/><code> 'Bucket'=&gt;$bucket,<\/code><br \/><code><code> 'Key' =&gt; '<\/code><\/code><strong>&lt;PATH To UPLOAD&gt;<\/strong><code>'.$filename, # &lt;- PATH to Upload. Eg.,Directory\/Subdirectory1<\/code><br \/><code> 'SourceFile' =&gt; $target_file,<\/code><br \/><code> ));<\/code><br \/><code> $message = \"S3 Upload Successful.\";<\/code><br \/><code> echo $message;<\/code><br \/><code> echo \"&lt;script&gt; alert('File Uploaded Successfully')&lt;\/script&gt;\";<\/code><br \/><code> #echo \"Delete the temporary file.\";<\/code><br \/><code> unlink($target_file);<\/code><br \/><code> }<\/code><br \/><code> catch (S3Exception $e) {<\/code><br \/><code> \/\/ Catch an S3 specific exception.<\/code><br \/><code> echo $e-&gt;getMessage();<\/code><br \/><code> }<\/code><br \/><code> }<\/code><br \/><code> else<\/code><br \/><code> {<\/code><br \/><code> echo \"Upload Failed\";<\/code><br \/><code> }<\/code><\/p>\n<p><code>}<\/code><br \/><code> else<\/code><br \/><code> {<\/code><br \/><code> echo \"File is greater than 1MB\";<\/code><br \/><code> }<\/code><br \/><code> }<\/code><br \/><code> else<\/code><br \/><code> {<\/code><br \/><code> echo \"Please Select the file\";<\/code><br \/><code> }<\/code><\/p>\n<p><code>}<\/code><br \/><code>?&gt;<\/code><\/p>\n<p><code>&lt;html&gt;<\/code><br \/><code>&lt;head&gt;<\/code><br \/><code> &lt;title&gt;Pheonix solutions S3 Image upload &lt;\/title&gt;<\/code><br \/><code> &lt;style type=\"text\/css\"&gt;<\/code><br \/><code> .fileUpload {<\/code><br \/><code> position: relative;<\/code><br \/><code> overflow: hidden;<\/code><br \/><code> margin: 10px;<\/code><br \/><code> }<\/code><br \/><code> .fileUpload input.upload {<\/code><br \/><code> position: absolute;<\/code><br \/><code> top: 0;<\/code><br \/><code> right: 0;<\/code><br \/><code> margin: 0;<\/code><br \/><code> padding: 0;<\/code><br \/><code> font-size: 20px;<\/code><br \/><code> cursor: pointer;<\/code><br \/><code> opacity: 0;<\/code><br \/><code> filter: alpha(opacity=0);<\/code><br \/><code>}<\/code><br \/><code> body {<\/code><br \/><code> background-color: lightgrey;<\/code><br \/><code>}<\/code><br \/><code>h1 {<\/code><br \/><code> color: navy;<\/code><br \/><code> margin-left: 20px;<\/code><br \/><code>}<\/code><br \/><code>.btn &gt; .caret,<\/code><br \/><code> .dropup &gt; .btn &gt; .caret {<\/code><br \/><code> border-top-color: #000 !important;<\/code><br \/><code> }<\/code><br \/><code> .btn-success,<\/code><br \/><code>.btn-green {<\/code><br \/><code> color: #ffffff;<\/code><br \/><code> background-color: #00a651;<\/code><br \/><code> border-color: #00a651;<\/code><br \/><code>}<\/code><\/p>\n<p><code>.btn-file {<\/code><br \/><code> overflow: hidden;<\/code><br \/><code> position: relative;<\/code><br \/><code> vertical-align: middle;<\/code><br \/><code>}<\/code><br \/><code>.fileinput-exists .fileinput-new, .fileinput-new .fileinput-exists {<\/code><br \/><code> display: none;<\/code><br \/><code>}<\/code><br \/><code>.fileinput .thumbnail {<\/code><br \/><code> overflow: hidden;<\/code><br \/><code> display: inline-block;<\/code><br \/><code> margin-bottom: 5px;<\/code><br \/><code> vertical-align: middle;<\/code><br \/><code> text-align: center;<\/code><br \/><code>}<\/code><br \/><code>.panel-primary {<\/code><br \/><code> border-color: #ebebeb;<\/code><br \/><code> -webkit-border-radius: 3px;<\/code><br \/><code> -webkit-background-clip: padding-box;<\/code><br \/><code> -moz-border-radius: 3px;<\/code><br \/><code> -moz-background-clip: padding;<\/code><br \/><code> border-radius: 3px;<\/code><br \/><code> background-clip: padding-box;<\/code><br \/><code>}<\/code><\/p>\n<p><code>body {<\/code><br \/><code> font-family: \"Helvetica Neue\", Helvetica, \"Noto Sans\", sans-serif, Arial, sans-serif;<\/code><br \/><code> font-size: 12px;<\/code><br \/><code> line-height: 1.42857143;<\/code><br \/><code> color: #949494;<\/code><br \/><code> background-color: lightgrey;<\/code><br \/><code>}<\/code><br \/><code> &lt;\/style&gt;<\/code><br \/><code>&lt;\/head&gt;<\/code><br \/><code>&lt;body&gt;<\/code><br \/><code>&lt;form action=\"\" method='post' enctype=\"multipart\/form-data\"&gt;<\/code><br \/><code> &lt;h1&gt;&lt;center&gt;Image Upload DashBoard! Build by Pheonix Solutions&lt;\/center&gt;&lt;\/h1&gt;<\/code><br \/><code> &lt;div class=\"main-content\"&gt;<\/code><br \/><code> &lt;div class=\"panel panel-primary \"&gt;<\/code><br \/><code> &lt;label&gt; Please Select the image to upload&lt;\/label&gt;&lt;br&gt;<\/code><br \/><code> &lt;input type='file' class=\"fileinput thumbnail btn btn-file\" name='s3file'\/&gt; &lt;br&gt;<\/code><br \/><code> &lt;\/div&gt;<\/code><br \/><code> &lt;input type='submit' class=\"btn btn-success\" name='upload' value='Upload Image'\/&gt;<\/code><br \/><code> &lt;\/div&gt;<\/code><\/p>\n<p><code>&lt;\/body&gt;<\/code><br \/><code>&lt;\/html&gt;<\/code><\/p>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using the AWS SDK for PHP provides a simple way to upload files from a PHP application directly to an Amazon S3 bucket. The script temporarily stores the uploaded file on the server, uploads it to the specified S3 location, and removes the temporary file after a successful upload. For production environments, avoid hardcoding AWS access keys in the PHP script and use IAM roles, environment variables, or another secure credential mechanism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the maximum file size supported by this script?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The script allows files smaller than <strong>1 MB<\/strong>. Files larger than 1 MB are rejected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Why is Composer required for this script?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Composer is used to install and manage the <strong>AWS SDK for PHP<\/strong>, which provides the required S3 functionality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Can AWS access keys be hardcoded in the PHP script?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Although the script demonstrates access and secret keys directly in the configuration, <strong>hardcoding credentials is not recommended for production<\/strong>. Use IAM roles, environment variables, or another secure credential-management method instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/automate-mysqldump-backup-from-serverbackend-to-amazon-s3\/\">Automate MySQLdump Backup from server(backend) to Amazon S3<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business?. Our experts can help with\u00a0web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team <a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Amazon S3 is a scalable object storage service that can be used to store and manage files securely. This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-by","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=716"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":11700,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/716\/revisions\/11700"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}