如何透過PHP 上傳視訊檔案並將其保存在具有資料庫條目的適當資料夾中
問題:
使用者需要能夠將視訊檔案上傳到網站,並將其儲存在指定的資料夾中,並具有相應的資料庫條目以供使用者識別。
解決方案:
HTML 表單
定義使用者選擇要上傳的影片檔案的 HTML 表格。確保表單包含用於文件選擇和提交的適當屬性。
<code class="html"><form method="post" enctype="multipart/form-data"> <input type="file" accept="video/*" ID="fileSelect" name="filename"> <select name="course"> <option value="select" selected>Select</option> <option value="java">Java</option> <option value="python">Python</option> <option value="vb">Visual Basic</option> <option value="c">C/C++</option> <option value="ruby">Ruby</option> </select> <input type="submit" value="Upload" name="submit"> </form></code>
PHP 處理程序
使用PHP 處理上傳的視訊檔案:
<code class="php">// Define destination folder based on selected course $folder = isset($_POST["course"]); // Destination folder path $destination_folder = '/' . $folder; // Maximum file size (in bytes) $max_file_size = 0; // Success URL for redirection after upload $success_url = 'learn.learnbrix.com'; // Upload success log database connection details (optional) define('DB_HOST',''); // host, usually localhost define('DB_DATABASE',''); // database name define('DB_USERNAME',''); // username define('DB_PASSWORD',''); // password // Check for upload errors $error_code = $_FILES['filename']['error']; if ($error_code !== UPLOAD_ERR_OK) { // Handle upload errors as needed } // Process the uploaded file $filename = $_FILES['filename']['name']; $tmp_filename = $_FILES['filename']['tmp_name']; $file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); $filesize = filesize($tmp_filename); // Check file size and type if ($filesize > $max_file_size) { // Handle file size error } if (!in_array($file_ext, array('mp4', 'avi', 'mov'))) { // Handle file type error } // Save the file in the destination folder $dest_filename = md5(uniqid(rand(), true)) . '.' . $file_ext; move_uploaded_file($tmp_filename, $destination_folder . '/' . $dest_filename); // Log the upload (optional) if (DO_LOG) { // Connect to the database and execute the query to log the upload } // Redirect to the success URL header('Location: ' . $success_url);</code>
其他功能
其他功能要新增接受使用者姓名和電子郵件地址的功能,請相應修改HTML 表單和PHP 處理程序:
<code class="html"><form method="post" enctype="multipart/form-data"> <label for="name">Name:</label> <input type="text" name="name"> <label for="email">Email:</label> <input type="email" name="email"> <input type="file" accept="video/*" ID="fileSelect" name="filename"> <select name="course"> <option value="select" selected>Select</option> <option value="java">Java</option> <option value="python">Python</option> <option value="vb">Visual Basic</option> <option value="c">C/C++</option> <option value="ruby">Ruby</option> </select> <input type="submit" value="Upload" name="submit"> </form></code>
<code class="php">// Get the user's name and email from the form $name = $_POST['name']; $email = $_POST['email']; // Use $name and $email to create a database entry or perform any other necessary action</code>
以上是如何在 PHP 中上傳視訊檔案並透過資料庫整合進行分類?的詳細內容。更多資訊請關注PHP中文網其他相關文章!