简体   繁体   中英

PHP upload HTML file and display link on index

I am currently developing a website where users can upload and share there websites. I have tried to create a upload feature but failed every attempt. I just want a file upload system, preferably created with PHP, that will upload a visitors HTML page and will automatically put the link of there page somewhere on the index.html page.

Here is the code for 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>

Here is the code for 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"];
    }
}
?>

Please help.

You're asking a bit odd questions to say the least. Your code is not consistent with your questions or request either. Automatically upload HTML? And yet you have a form to store user website HTML?

Well user will have to provide you with their website HTML and also their website URL. Which means you need to adjust your script to handle both options. Yet I only see one option, to upload HTML. And there is nothing automatic about it either. It's just a simple upload text function. If you want a real automatic version? Then you should think outside the box. You could for example let users provide their URL and then where you make some sort of a bot to go and take screenshots of that website layout. Also using a simple JS you could add the option to write user website url. Then below an iframe will be undated loading user url. Then you could use https://html2canvas.hertzen.com/ version 1.0.0-rc.1 to make an approximate snapshot of user url layout.

Who otherwise in their right mind would upload their entire HMTL? For that you need a lot more work than what you've provided. A website is not only HMTL. Without JavaScript these days there is no website. Because almost every website layout will break down without JavaScript. Then there are images without the full url since images are usually loaded locally or even trough AWS or something.

Anyways, you need to go back to the drawing board and figure out what exactly you need. I would suggest that you need people to upload an archive with the entire website if that is what you need, an entire website?

But if you ask wrong questions, then you will receive wrong answers. There is no way in the world this code you've provided will be sufficient for anything useful other than some testing purposes.

I have Updated the code and now you can try it You have used file size in in proper way And it cause errors

 <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM