Upload file on s3 AWS using PHP

Upload file on s3 AWS using PHP

Our Next Topic is on AWS server, in this tutorial we are assuming that you are already aware with aws,in this tutorial we are going to demonstrate that how to upload files to s3 from EC2 or from your local machine so let's start.
     Please follow following steps

     1. Create new direct on your local sever and paste all the downloaded files.
     2. Create form for file uploed in that directyory and paste following code .
  1. <?php
  2.             include('checkimg.php'); // getExtension Method
  3.             include('s3_config.php');
  4.             $msg='';
  5.             if($_SERVER['REQUEST_METHOD'] == "POST")
  6.             {
  7.             $name = $_FILES['file']['name'];
  8.             $size = $_FILES['file']['size'];
  9.             $tmp = $_FILES['file']['tmp_name'];
  10.             $ext = getExtension($name);
  11.             if(strlen($name) > 0)
  12.             {
  13.             // File format validation
  14.             if(in_array($ext,$valid_formats))
  15.             {
  16.             // File size validation
  17.             if($size<(1024*1024))
  18.             {
  19.             //Rename image name.
  20.             $actual_image_name = time().".".$ext;
  21.             if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
  22.             {
  23.             $msg = "S3 Upload Successful.";
  24.             $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
  25.             echo "<img src='$s3file'/>";
  26.             echo 'S3 File URL:'.$s3file;
  27.             }
  28.             else
  29.             $msg = "S3 Upload Fail.";
  30.             }
  31.             else
  32.             $msg = "Image size Max 1 MB";
  33.             }
  34.             else
  35.             $msg = "Invalid file, please upload image file.";
  36.             }
  37.             else
  38.             $msg = "Please select image file.";
  39.             }
  40. ?>
  41. <form action="" method='post' enctype="multipart/form-data">
  42. Upload image file here
  43. <input type='file' name='file'/> <input type='submit' value='Upload Image'/>
  44. <?php echo $msg; ?>
  45. </form>

Post a Comment

Previous Post Next Post