【发布时间】:2021-07-12 10:39:51
【问题描述】:
我目前正在开发一个网站,用户可以在那里上传和分享网站。 我试图创建一个上传功能,但每次尝试都失败了。 我只想要一个文件上传系统,最好用 PHP 创建,它会上传访问者的 HTML 页面,并会自动将该页面的链接放在 index.html 页面的某个位置。
这里是 index.html 的代码:
<!DOCTYPE html>
<html>
<body>
<style>
* {
font-family: sans-serif;
}
</style>
<h1>File Upload</h1>
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input type="file" name="html"> <br/> <br/>
<input type="submit" value="Upload">
</form>
</body>
<br/> <br/>
<body>
<a href="https://example.com/">Example Link</a> <!-- Link of Uploaded file -->
</body>
</html>
这里是upload.php的代码:
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_FILES["html"]) && $_FILES["html"]["error"] == 0){
$allowed = array("html", "htm");
$filename = $_FILES["html"]["name"];
$filetype = $_FILES["html"]["type"];
$filesize = $_FILES["html"]["size"];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Sorry, the file you selected is not supported");
$mazsize = 40MB;
if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
if(in_array($filetype, $allowed)){
if(file_exists("upload/" . $filename)){
echo $filename . " is already exists.";
} else{
move_uploaded_file($_FILES["html"]["tmp_name"], "upload/" . $filename);
echo "Success!";
}
} else{
echo "An error occured while uploading your file.";
}
} else{
echo "Error: " . $_FILES["html"]["error"];
}
}
?>
请帮忙。
【问题讨论】:
-
请展示您在 PHP 中所做的事情(展示您的 PHP 代码)
-
我刚刚添加了 PHP。
-
您的表单中缺少最重要的部分
enctype="multipart/form-data"。加上method="post" -
另一个重要的一点:您需要为您的文件输入框提供一个名称。所以 应该是
标签: php html file-upload