php - Remote Upload Amazon S3 -
i have made script upload remote files link amazon s3 not work. have no idea what's wrong. below code done , i'm using amazon sdk in 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 not exists'; } } else { echo 'invalid url'; } } else { echo 'please enter url'; } ?>
and index.php
<html> <head> <title>php file upload 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> 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 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()'> <img src='ajax-loader.gif' id='1'> <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>
the best way debug php run via command line version of php, show errors in terminal instead of digging through webserver logs may or may not give details.
Comments
Post a Comment