PHP 上传 HTML 文件并在索引中显示 link

PHP upload HTML file and display link on index

我目前正在开发一个网站,用户可以在其中上传和共享网站。 我试图创建一个上传功能,但每次尝试都失败了。 我只想要一个文件上传系统,最好用 PHP 创建,它将上传一个访问者 HTML 页面,并自动将那里的 link 页面放在 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
// 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 = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        if(!in_array($ext, $allowed)) die("Sorry, the file you selected is not supported");
        if($filesize > 4000000) die("Error: File size is larger than the allowed limit.");
        if(file_exists("upload/" . $filename)){
            echo $filename . " is already exists.";
        } else{
            if(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"];
    }
}
?>
<!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>

至少可以说,您问的问题有点奇怪。您的代码与您的问题或要求不一致。自动上传HTML?然而你有一个表格来存储用户网站 HTML?

用户必须向您提供他们的网站 HTML 以及他们的网站 URL。这意味着您需要调整脚本以处理这两个选项。然而我只看到一个选项,上传 HTML。而且它也没有什么自动的。这只是一个简单的上传文本功能。如果你想要一个真正的自动版本?那么你应该跳出框框思考。例如,您可以让用户提供他们的 URL,然后您可以在其中制作某种机器人并截取该网站布局的屏幕截图。同样使用简单的 JS,您可以添加写入用户网站的选项 url。那么下面一个iframe将会是未注明日期的加载用户url。然后你可以使用 https://html2canvas.hertzen.com/ 版本 1.0.0-rc.1 来制作用户 url 布局的近似快照。

如果头脑正常,谁会上传整个 HMTL?为此,您需要做的工作比您提供的要多得多。网站不仅是 HMTL。这些天没有 JavaScript 就没有网站。因为几乎每个网站布局都会在没有 JavaScript 的情况下崩溃。然后是没有完整 url 的图像,因为图像通常在本地加载,甚至通过 AWS 或其他方式加载。

无论如何,您需要回到绘图板并弄清楚您到底需要什么。如果您需要整个网站,我建议您需要人们上传整个网站的存档?

但是如果你问错了问题,那么你会得到错误的答案。除了某些测试目的之外,您提供的这段代码无法满足任何有用的需求。