File uploads on a web page

From XPUB & Lens-Based wiki
Revision as of 15:34, 24 February 2026 by Manetta (talk | contribs) (→‎uploads/)

There are different ways how you can do file uploads actually. It's something you totally can have a personal preference for ;)

At XPUB i have seen:

  • Michael using a CGI folder + a python script, which he has experience with to set up on a server
  • Manetta using PHP and struggle with nginx configurations
  • Kamo making dozens of DIWO Flask applications with upload functionalities

As we will work with Flask during prototyping at some point, our suggestion would be to look into the Flask option, so we can continue with it in class. But it's up to you of course :--)

Documentation for the file upload forms:

A bit of a background story!

The beauty (Flask) and the beast (PHP)
aka "Uploading Files is my Passion <3"

When it comes to writing (small) web applications, kamo and i both enjoy working with Flask. We explicitly phrase it in this way, because it feels important to phrase it as a personal preference, an opinion that we grew into over time, and might change over time again!

Flask is part of the modern world of "server-side web application frameworks", is written in Python, and exist as a project since 2010. Flask is developed by the Pallets organization, which is a team of 4 people apparently + community around it, and is funded by the Python Software Foundation. Bigger current applications that run on Flask include Pinterest and LinkedIn (yep).

Flask is one of the many other frameworks that are out there, such as Django (written in Python, used by Instagram) and Rails (written in Ruby, used by AirBnB/Archives of Our Own/Twitch/Github).

You use these server-side frameworks to create dynamics environments on the web that control dynamic interaction between a web page and a server.

When the web emerged in the early 1990s, only static pages could visited. But then, in 1993, the "Common Gateway Interface (CGI)" was added as a new feature that allowed (so-called) "dynamic interactions" such as file uploads and database queries. Quickly after, in 1995, the (so-called) "fully integrated server/language development environment" PHP was introduced. It triggered the making of cute guestbooks, small photo gallery software projects, but also account based environments that more and more would ask for your (so-called) user input that grew into social media platforms, web shops, online work environments, etc etc etc.

Flask vs PHP

Anyways!

While preparing this file uploads example, we ended up in a heated debate about what to prepare for you between Flask and PHP (and for a tiny tiny moment also CGI). Let's do Flask, no let's do PHP, no Flask, nooo PHP. And we ended up with Flask.

Because we think that navigating these options yourself if the ultimate goal of prototyping, here a summary of the pro's and con's:

Flask

  • + web application framework we want to introduce in XPUB1 prototyping anyway
  • + comes with a lot of possibilities
  • - each flask application requires configuration on multiple layers: a flask application + nginx configuration + background process, which makes it a layered steap learning process
  • - using Flask require access to a full operating system, and thus requires students to have their own server if they want to experiment with it outside of sergio

PHP

  • + PHP is widely supported by web hosting providers and thus does not require access to a full operating system (like Flask does)
  • + duplicating the upload example is super easy, you just copy paste the PHP script and HTML example prepared by us (or found online)
  • + PHP is integrated within HTML files, it runs in the same environment
  • - configuring PHP on sergio in advance hides the difficult layers to students
  • - we as tutors have limited experience with PHP and thus cannot easily adapt and customize the upload script

Flask example

This example uses the following files and folder:

myuploadform
├── main.py
├── templates
│   └── upload.html
└── uploads
  1. initiate a new uv folder: uv init myuploadform
  2. move into this folder: cd myuploadform and create a venv: uv venv
  3. install flask: uv add flask
  4. save the code from below as main.py and upload.html
  5. make a uploads folder: mkdir uploads
  6. run the flask application by running the python script: uv run main.py
  7. see the flask application in your browser: http://localhost:9000

main.py

from flask import Flask, request, render_template
from prefix import PrefixMiddleware

app = Flask(__name__)
app.wsgi_app = PrefixMiddleware(app.wsgi_app, '/sergio/flask-upload/')
upload_folder = 'uploads/'

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        file = request.files['file']
        if file.filename != '':
            file.save(upload_folder + file.filename)
        return redirect(url_for('home'))
    return render_template('upload.html')

if __name__ == "__main__":
    app.run(port=9000, debug=True)

upload.html

<!DOCTYPE html>
<html>
<body>

<form method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="Upload">
</form>

</body>
</html>