dtdfl62844 2015-08-30 23:49
浏览 104

远程上传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>
  • 写回答

1条回答 默认 最新

  • douquan9826 2015-08-31 01:38
    关注

    The best way to debug php is to run it via the command line version of php, this will show you your errors in the terminal instead of digging through webserver logs which may or may not give you any details.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用