Golang vs PHP File Server

Source: Internet
Author: User

Previous words

The author is Golang brain remnant powder, this content may cause phper discomfort, please read carefully!

Two days ago a colleague encountered a problem, need a can support upload, download the function of the HTTP server to do a data center. I just did, so I promised to give him a lift.

HTTP server, the first thought is PHP + nginx. So open the first, write a PHP upload

<?php    if ($_FILES["file"]["error"] > 0)    {        echo "错误:: " . $_FILES["file"]["error"] . "<br>";    }    else    {        if (file_exists("upload/" . $_FILES["file"]["name"]))        {            echo $_FILES["file"]["name"] . " 文件已经存在。 ";        }        else        {            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);            echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];        }    }?>

All right, write it! The demand is complete! Test it out!

So start the first test, the result: Failure!

The reason is that PHP upload_max_filesize only 2 m, the uploaded file size exceeds the limit.
Modified the php.ini configuration, re-test can be uploaded

Then deploy to the server and put the. There is a Openresty (Nginx Series Web server) on the server, throw the upload.php file inside, and then restart the service. OK, you can test it again!

So the second test, the result: Failure!

The reason is that openresty default does not open PHP parsing, to change the configuration. Open the PHP parsing in the nginx.conf. Restart Nginx, then test it again ~

So, the third Test, or failure!

Originally.. This machine, although has nginx, but did not install php!!! Think about going to the extranet to download PHP, and then choose the version, and then come back to install also configure environment variables and openresty associated with PHP configuration.

Forget it, bye php!.

It's time for the go language to play!!

In the world of Golang, 1 lines of code can handle a file server.

package mainimport (    "log"    "net/http")func main() {    log.Fatal(http.ListenAndServe(":8038", http.FileServer(http.Dir("./"))))}

In this way, you can access the 8038 port on the local computer to download the file of the specified path! No need to rely on Nginx or any other Web server

Fileserver.go all the code that contains the upload and download functions are as follows

package mainimport (    "fmt"    "io"    "log"    "net/http"    "os")const (    uploadPath = "./Files/")func main() {    http.HandleFunc("/upload", uploadHandle)    fs := http.FileServer(http.Dir(uploadPath))    http.Handle("/Files/", http.StripPrefix("/Files", fs))    log.Fatal(http.ListenAndServe(":8037", nil))}func uploadHandle(w http.ResponseWriter, r *http.Request) {    file, head, err := r.FormFile("file")    if err != nil {        fmt.Println(err)        return    }    defer file.Close()    filePath := uploadPath + head.Filename    fW, err := os.Create(filePath)    if err != nil {        fmt.Println("文件创建失败")        return    }    defer fW.Close()    _, err = io.Copy(fW, file)    if err != nil {        fmt.Println("文件保存失败")        return    }    io.WriteString(w, "save to "+filePath)}
How to Deploy

Go is a statically compiled language that compiles executable files directly on Windows, which is exe. On any machine, no need to install additional environment, you can run directly!

So compile the FileServer.exe file and throw it on the server.

Continue testing! Result: success, Steady!

Golang vs PHP File Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.