【发布时间】:2020-11-24 11:45:05
【问题描述】:
您好,我正在尝试在 php 中创建上传表单。包含、上传输入、文本输入(上传文件的名称)和提交按钮。但是我对php, 了解不多,所以我不知道如何实际链接我在<input type="text"/> 上输入的内容在上传时成为文件的名称。如果有人可以帮忙?谢谢。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
【问题讨论】:
-
在 PHP 中提交后如何访问普通表单字段,基本上任何初学者教程都可以阅读,以防您仍然不知道。并且文件的目标名称在
move_uploaded_file调用中设置。