我正在用PHP(Laravel 4框架)构建一个应用程序,他们帐户中的老师可以为学生创建数字课程。数字课程可以包含以下内容:
- 文本
- (表单、.doc、.txt、.pdf等文本)
- 图像(.gif、.png、.jpg等)
- 视频(.avi、.mov、.mp4等)
- 音频(.mp3等)
从表单输入的原始文本显然可以针对lesson_id存储在数据库中。所有其他内容格式都需要存储在某个地方,在那里我可以管理和读取文件,并跟踪教师的存储总数,因为我计划按 5GB、10GB 等的存储阈值计费。
在创建课程页面上,在保存课程之前,需要将内容文件作为课程附件上传,以便教师可以直观地看到所有课程内容,然后点击保存以立即创建课程。
这是我想出的:
在创建课程之前,将所有课程文件附件上传到 AWS S3 到教师专用存储桶。将教师 ID 和日期时间添加到每个文件名。
如果所有上传的视频/音频文件不是iDevice友好格式或超过文件大小限制,则强制将其转换为.mp4,.mp3等。使用 FFmpeg 来执行此操作。
保存并创建课程后,根据数据库中的课程 ID 记录 S3 文件 URL。
如果课程在特定时间段后仍未创建,请运行 cron 作业以检查上传的没有课程的 S3 文件并将其删除。
我不确定解决此问题的最佳方法是什么,因为用户上传的内容管理对我来说真的很新。
您如何看待这种方法?您能推荐一种改进或更好的方法来解决此问题吗?
简短的回答 - 你建议的方法很好。
长答案...
我也有类似的情况,我允许用户上传他们正在宣传出售的商品的图像。虽然我不会有像你那么多的文件,也不会有许多不同文件类型的复杂性,但原理是一样的......
在我的表单中,使用户能够创建广告(在您的案例中是课程),我使用了一个拖放区(dropzone.js),用户可以拖放图像。这会提示对我的 Laravel API 进行 AJAX 发布调用,文件会立即上传(即在按下"提交"按钮之前)。它们上传到 tmp 目录。然后将此 tmp 目录的位置返回到浏览器,.js脚本将该位置存储在表单的隐藏字段中。当用户满意时,他点击提交并上传表单。此时,我处理表单并(除其他外)调整图像大小并将它们移动到其永久位置,路径存储在数据库中。然后删除 tmp 目录。
然后,我每天运行一次 cron 作业以清除仍然存在的任何 tmp 目录(这将由用户丢弃图像但随后未提交表单引起)。
我认为你的逻辑很好,我认为它可能会帮助你听到其他人如何处理工作流程。
祝你好运
<?php
// Set the max file size and upload path
$maxFileSize = (100 * 1024);
$uploadDir = 'upload/';
// Do not edit these values
$showForm = false;
$formError = false;
$uploadFilePath = '';
if (isset($_POST['submit'])) {
// Check for upload errors
if ($_FILES['file']['error'] > 0) {
$formError = 'Error: ' . $_FILES['file']['error'];
} else {
$fileName = $_FILES['file']['name'] ; // the name of the uploaded file
$fileType = $_FILES['file']['type'] ; // the type of the uploaded file
$fileSize = $_FILES['file']['size'] ; // the size in bytes of the uploaded file
$tempLocation = $_FILES['file']['tmp_name']; // the temp file location
// Check the file type
if ( ! in_array($fileType, array('image/jpeg', 'image/png'))) {
$formError = 'Invalid file type. Must be jpeg or png.';
}
// Check the file size
elseif ($fileSize > $maxFileSize || $fileSize < 0) {
$formError = 'Invalid file size. Must be between 1 and ' . $maxFileSize . ' kb.';
}
// Make sure the file does not exist
elseif (file_exists($uploadDir . $fileName)) {
$formError = 'The file "' . $fileName . '" already exists.';
}
// The file is valid so continue with the upload
else {
// Move the file from the tmp dir to the desired location
move_uploaded_file($tempLocation, $uploadDir . $fileName);
// Remember the complete upload file path
$uploadFilePath = $uploadDir . $fileName;
// Store the upload information in the database
$c = mysql_connect('localhost','root','') or die (mysql_error());
$d = mysql_select_db('datebase_name') or die(mysql_error());
mysql_query("insert into table_name values('','$fileName','$fileType','$fileSize')") or die(mysql_error());
}
}
} else {
$showForm = true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<?php
if ($showForm) {
if ($formError !== false) {
echo '<p>' . $formError . '</p>';
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
<?php
} else {
echo '<p>File stored at: ' . $uploadFilePath . '</p>';
}
?>
</body>
</html>
在这里,文件将存储在名为"upload"的文件夹中,上传的文件大小和类型保存在数据库中。
在上面的示例中,数据库表有 4 列(按此顺序):id、文件名、文件类型和文件大小。
注意:这是一个基本示例。不要忘记改进文件验证以增强安全性。最好不要向用户显示上传位置。在大多数情况下,像"文件上传成功"这样的上传确认就足够了。