通过PHP将文件从网站上传到Amazon EC2服务器 [英] Upload file to Amazon EC2 server from website by PHP
本文介绍了通过PHP将文件从网站上传到Amazon EC2服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网站(bedatify.com),我想创建一个页面,人们可以在其中将其图像上传到我的Amazon EC2服务器.我检查了类似的问题无法在Amazon EC2-php 和如何将文件上传到Amazon EC2 但我不知道如何管理!这一段f代码是一个好的开始吗?我该怎么做才能让用户将图片从我的网站直接上传到我的EC2服务器?
I have a website ( bedatify.com) and I want to make a page within which people could upload their images to my amazon EC2 server. I checked similar questions like Unable to upload files on Amazon EC2 - php and how to upload to files to amazon EC2 but I don't figure it out how to manage it! Is this piece f code a good start? What should I change to let user upload pictures directly to my EC2 server from my website?
<?php
if(isset($_POST['image'])){
echo "in";
$image = $_POST['image'];
upload($_POST['image']);
exit;
}
else{
echo "image_not_in";
exit;
}
function upload($image){
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = "pleeease";
$upload_folder = "/var/www/html/upload";
$path = "$upload_folder/$id.jpg";
if(file_put_contents($path, base64_decode($image)) != false){
echo "uploaded_success"
}
else{
echo "uploaded_failed";
}
}
?>
推荐答案
uploadfile.php
uploadfile.php
<?php
$IAM_KEY = 'xxxx';
$IAM_SECRET = 'xxxx';
$bucket = 'xxxx';
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => $IAM_KEY,
'secret' => $IAM_SECRET
]
]);
$file = $_FILES["fileToUpload"]["tmp_name"];
try {
// Upload data.
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => 'xxx',
'SourceFile' => $file
]);
// Print the URL to the object.
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
index.html
index.html
<form action="/AWS/uploadfile.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
这篇关于通过PHP将文件从网站上传到Amazon EC2服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文