如何允许用户上传文件到网站?
How to allow user to upload files to website?
我有一个用 HTML/CSS/Vanilla JS 编写的网站,我想添加一个表单,供用户将图片和文本上传到根文件夹。似乎是一项平凡的任务,但我找不到这方面的任何信息。我应该使用什么技术?
使用 <input type="text"/>
和 <input type="file"/>
,您可以让用户输入文本和文件(图片适合您的情况)。
现在,要将此信息保存在您的文件中,您应该使用 php 或其他一些服务器端技术。
PHP 方法:https://www.allphptricks.com/upload-file-using-php-save-directory/
对于纯文本:How can I save my form data to a local text file using php?
这是您进入后端开发领域的地方:您可以(例如)使用 HTML <input type="file">
到 select 文件并将其作为表单,但您需要一个服务器来处理您的请求。有很多语言可以用来编写后端服务器,一些流行的是节点(服务器端 JS)和 PHP.
你可以看看这个,我希望它能起作用,请确保创建一个文件夹来存储上传的视频。
文件 1 -> config.php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = "admin"; /* Password */
$dbname = "mydb"; /* Database name */
// Create connection
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
文件 2 -> index.php 这将是文件的主页
<!doctype html>
<html>
<head>
<?php
include("config.php");
if(isset($_POST['but_upload'])){
$maxsize = 10000000; // 5MB
$name = $_FILES['file']['name'];
$target_dir = "https://mmcollegebihiya.in/form1/videos/";
$target_file = $target_dir . $_FILES["file"]["name"];
// Select file type
$videoFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("mp4","avi","3gp","mov","mpeg");
// Check extension
if( in_array($videoFileType,$extensions_arr) ){
// Check file size
if(($_FILES['file']['size'] >= $maxsize) || ($_FILES["file"]["size"] == 0)) {
echo "File too large. File must be less than 5MB.";
}else
{
// Upload
//if(move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){
// Insert record
$query = "INSERT INTO videos(name,location) VALUES('".$name."','".$target_file."')";
mysqli_query($con,$query);
echo "Upload successfully.";
//}
}
}else{
echo "Invalid file extension.";
}
}
?>
</head>
<body>
<form method="post" action="" enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' value='Upload' name='but_upload'>
</form>
</body>
</html>
文件 3 -> 检查视频 php
<?php
include("config.php");
?>
<!doctype html>
<html>
<head>
<style>
video{
float: left;
}
</style>
</head>
<body>
<div>
<?php
$fetchVideos = mysqli_query($con, "SELECT * FROM videos ORDER BY id DESC");
while($row = mysqli_fetch_assoc($fetchVideos)){
$location = $row['location'];
echo "<div >";
echo "<video src='".$location."' controls width='320px' height='200px' >";
echo "</div>";
}
?>
</div>
</body>
</html>
如果不行,我会分享其他代码,但不要标记-1。
我有一个用 HTML/CSS/Vanilla JS 编写的网站,我想添加一个表单,供用户将图片和文本上传到根文件夹。似乎是一项平凡的任务,但我找不到这方面的任何信息。我应该使用什么技术?
使用 <input type="text"/>
和 <input type="file"/>
,您可以让用户输入文本和文件(图片适合您的情况)。
现在,要将此信息保存在您的文件中,您应该使用 php 或其他一些服务器端技术。
PHP 方法:https://www.allphptricks.com/upload-file-using-php-save-directory/
对于纯文本:How can I save my form data to a local text file using php?
这是您进入后端开发领域的地方:您可以(例如)使用 HTML <input type="file">
到 select 文件并将其作为表单,但您需要一个服务器来处理您的请求。有很多语言可以用来编写后端服务器,一些流行的是节点(服务器端 JS)和 PHP.
你可以看看这个,我希望它能起作用,请确保创建一个文件夹来存储上传的视频。
文件 1 -> config.php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = "admin"; /* Password */
$dbname = "mydb"; /* Database name */
// Create connection
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
文件 2 -> index.php 这将是文件的主页
<!doctype html>
<html>
<head>
<?php
include("config.php");
if(isset($_POST['but_upload'])){
$maxsize = 10000000; // 5MB
$name = $_FILES['file']['name'];
$target_dir = "https://mmcollegebihiya.in/form1/videos/";
$target_file = $target_dir . $_FILES["file"]["name"];
// Select file type
$videoFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("mp4","avi","3gp","mov","mpeg");
// Check extension
if( in_array($videoFileType,$extensions_arr) ){
// Check file size
if(($_FILES['file']['size'] >= $maxsize) || ($_FILES["file"]["size"] == 0)) {
echo "File too large. File must be less than 5MB.";
}else
{
// Upload
//if(move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){
// Insert record
$query = "INSERT INTO videos(name,location) VALUES('".$name."','".$target_file."')";
mysqli_query($con,$query);
echo "Upload successfully.";
//}
}
}else{
echo "Invalid file extension.";
}
}
?>
</head>
<body>
<form method="post" action="" enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' value='Upload' name='but_upload'>
</form>
</body>
</html>
文件 3 -> 检查视频 php
<?php
include("config.php");
?>
<!doctype html>
<html>
<head>
<style>
video{
float: left;
}
</style>
</head>
<body>
<div>
<?php
$fetchVideos = mysqli_query($con, "SELECT * FROM videos ORDER BY id DESC");
while($row = mysqli_fetch_assoc($fetchVideos)){
$location = $row['location'];
echo "<div >";
echo "<video src='".$location."' controls width='320px' height='200px' >";
echo "</div>";
}
?>
</div>
</body>
</html>
如果不行,我会分享其他代码,但不要标记-1。