dimanche 30 août 2015

Remote Upload Amazon S3

I have made an script to upload remote files by link to amazon s3 but does not work. I have no idea what's wrong. Below is all the code done and I'm using the Amazon SDK in its latest version:

config.php

<?php

return [
    's3' => [
        'key' => 'mykey',
        'secret' => 'mykey',
        'bucket' => 'mybucket',
        'region' => 'us-west-2',
        'version' => 'latest'
    ]
]

?>

start.php

<?php
use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';

$config = require('config.php');

//S3

$s3 = S3Client::factory([
    'key' => $config['s3']['key'],
    'secret' => $config['s3']['secret'],
    'region' => $config['s3']['region'],
    'version' => $config['s3']['version']

]);
?>

upload.php

<?php

use Aws\S3\Exception\S3Exception;
require 'start.php';

error_reporting(0);
$get_url = $_POST["url"];
$url = trim($get_url);
if($url)
{
    $file = fopen($url,"rb");
    $directory = "animes";
    $valid_exts = array("php","jpeg","gif","png","doc","docx","jpg","html","asp","xml","JPEG","bmp"); 
    $ext = end(explode(".",strtolower(basename($url))));
    if(in_array($ext,$valid_exts))
    {
        $rand = rand(1000,9999);
        $filename = "$rand.$ext";
        $newfile = fopen($directory . $filename, "wb");
        try{
            $s3->putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => "{$directory}/{$filename}",
                'ACL' => 'public-read'
        ]);


    } catch(S3Exception $e){
        die("Error.");
    }
        if($newfile)
        {
            while(!feof($file))
            {
                fwrite($newfile,fread($file,1024 * 8),1024 * 8);
            }
            echo 'File uploaded successfully';
            echo '**$$**'.$filename;
        }
        else
        {
            echo 'File does not exists';
        }
    }
    else
    {
        echo 'Invalid URL';
    }
}
else
{
    echo 'Please enter the URL';
}
?>

and index.php

<html>
    <head>
        <title>PHP File Upload From URL</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            $("#1").hide();
        });
        function uploadfile(){
            $("#1").show();
            $("#disp").html("");
            var url = encodeURIComponent($("#url").val());
            $.ajax({
                url: "upload.php",
                data: "url=" +url,
                type: 'post',
                success: function(data)
                {
                    var findsucc = data.indexOf("successfully");
                    out=data.split('**$$**');
                    if(findsucc!=-1)
                    {
                        $("#disp").css({"color": "green"});
                        $("#disp").html(out[0]);
                        $("#link").html("<a href='./upload/"+out[1]+"'>Click here</a> to view");
                        $("#1").hide();
                    }
                    else
                    {
                        $("#1").hide();
                        $("#disp").css({"color": "red"});
                        $("#disp").html(data);
                        $("#url").val("");
                    }
                }
            });

        }
        </script>
    </head>
<body>
<div align='center' style='padding-top: 40px;color: #4e4e4e;'><h1>PHP File Upload From URL</h1></div>
<div align='center' style='padding-top: 30px;'>
Enter Remote URL: <input type="text" name="url" id='url' size="35"> <input type="submit" value="Upload" name="submit" style='cursor: pointer;' onclick='uploadfile()'>&nbsp;&nbsp;<img src='ajax-loader.gif' id='1'>&nbsp;&nbsp;<br /><br /><div align='center'><span id='disp'></span></div><br>
<div id='link'></div><br />
<div style=" padding-left: 20px;font-size: 10px;color: #dadada;" id="dumdiv">
</div>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire