文件上传-使用php上传到S3并更新数据库


file upload - uploading to S3 using php and updating the database

我有一个S3 bucket,正在使用GitHub S3类,我成功上传了一个文件,但我还不知道上传后如何用文件路径和名称更新数据库。

我们将非常感谢在这个问题上提供的任何帮助。

编辑:GitHub中处理上传的代码

<?php
/**
* $Id$
*
* S3 form upload example
*/
if (!class_exists('S3')) require_once 'S3.php';
// AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'AccessKey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'SecretKey');
// Check for CURL
if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
    exit("'nERROR: CURL extension not loaded'n'n");
// Pointless without your keys!
if (awsAccessKey == 'change-this' || awsSecretKey == 'change-this')
    exit("'nERROR: AWS access information required'n'nPlease edit the following lines in this file:'n'n".
    "define('awsAccessKey', 'change-me');'ndefine('awsSecretKey', 'change-me');'n'n");

S3::setAuth(awsAccessKey, awsSecretKey);
$bucket = 'media-socialbeautyapp-com';
$path = ''; // Can be empty ''
$lifetime = 3600; // Period for which the parameters are valid
$maxFileSize = (1024 * 1024 * 50); // 50 MB
$metaHeaders = array('uid' => 123);
$requestHeaders = array(
    'Content-Type' => 'application/octet-stream',
    'Content-Disposition' => 'attachment; filename=${filename}'
);
$params = S3::getHttpUploadPostParams(
    $bucket,
    $path,
    S3::ACL_PUBLIC_READ,
    $lifetime,
    $maxFileSize,
    201, // Or a URL to redirect to on success
    $metaHeaders,
    $requestHeaders,
    false // False since we're not using flash
);
$uploadURL = 'https://' . $bucket . '.s3.amazonaws.com/';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>S3 Form Upload</title>
</head>
<body>
    <form method="post" action="<?php echo $uploadURL; ?>" enctype="multipart/form-data">
<?php
    foreach ($params as $p => $v)
        echo "        <input type='"hidden'" name='"{$p}'" value='"{$v}'" />'n";
?>
        <input type="file" name="file" />&#160;<input type="submit" value="Upload" />
    </form>
    <img src="https://media-socialbeautyapp-com.s3.amazonaws.com/Untitled-1.jpg" />
</body>
</html>

这段代码来自GitHub,可以上传文件,但我不知道如何让它也更新我的数据库服务器。

添加另一个名称为"success_action_redirect"的隐藏输入字段。该值是服务器上的回调URL。您可以将参数附加到回调URL,S3将在成功时调用它。不要忘记生成一个令牌并将其传递出去,以确保它是您的回调。

看起来你的代码是直接上传到S3的,所以你真的没有办法知道上传了什么。

考虑先将文件上传到本地服务器,写入数据库,然后上传到S3。

阅读inputFile()方法——它看起来像是可以用来将本地磁盘上的文件上传到S3的方法。

我现在正在做类似的事情。我暂时将它上传到我的服务器,写入数据库,然后将其推送到S3。然而,这实际上是两次上传,因此需要两倍的时间。

理想情况下,您希望添加一些自己的输入,并让amazon在POST变量保持不变的情况下重新引导回您的网站。然而,亚马逊不会让你这么做。所以它需要一些更聪明的东西。

不过我还没有解决,也许是AJAX 的问题

发现这非常有用,一次上传,工作完美。

S3教程