我有一个学校项目,我认为一切进展顺利,然后我部署它,发现用户无法上传图像到heroku上,因为它的临时文件系统。我的整个应用程序都以用户上传图像为中心。上传文件到S3在PHP中
我在某处读取AWS S3存储。所以我按照https://devcenter.heroku.com/articles/s3的说明操作。
我已经通过大部分的配置过程中消失,但在最后它说:
把下列句子变成一个文件名为index.php
<?php
require('vendor/autoload.php');
// this will simply read AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from env vars
$s3 = Aws\S3\S3Client::factory();
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
?>
<html>
<head><meta charset="UTF-8"></head>
<body>
<h1>S3 upload example</h1>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['userfile']) && $_FILES['userfile']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
// FIXME: add more validation, e.g. using ext/fileinfo
try {
// FIXME: do not use 'name' for upload (that's the original filename from the user's computer)
$upload = $s3->upload($bucket, $_FILES['userfile']['name'], fopen($_FILES['userfile']['tmp_name'], 'rb'), 'public-read');
?>
<p>Upload <a href="<?=htmlspecialchars($upload->get('ObjectURL'))?>">successful</a> :)</p>
<?php } catch(Exception $e) { ?>
<p>Upload error :(</p>
<?php } } ?>
<h2>Upload a file</h2>
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<input name="userfile" type="file"><input type="submit" value="Upload">
</form>
</body>
</html>
两个问题:
它不说哪里把index.php
。我假设它进入项目根目录。
我的其他问题是在代码本身。在第15行,他们说do not use 'name' for upload (that's the original filename from the user's computer)
我不确定这是什么意思,我怎么知道用户上传的文件名是什么?
你不会把这个放在'index.php'中的一个现有的应用程序中。这是一个独立的示例/演示应用程序。 –