file = request.files.get("image")
if file and allowed_file(file.filename):
# Sanitizes the filename and makes it secure
filename = secure_filename(file.filename)
# Assuming you have a function to generate the next product id or similar unique identifier
new_filename = f"product{get_next_product_id()}.{get_extension(filename)}"
# Make the file path compatible with operating system
file_path = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], new_filename)
file.save(file_path)
else:
image_error = "Invalid or no image provided"
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'png', 'jpg', 'jpeg', 'webp', 'gif'}
def get_extension(filename):
return filename.rsplit('.', 1)[1].lower()
def get_next_product_id():
# Logic to get the next product ID or maximum product ID from your database
result = db.execute("SELECT MAX(id) FROM products")
max_id = result[0]['MAX(id)']
return max_id + 1 if max_id else 1
# Just a dictionary, setting the path to the folder where the images will be uploaded
app.config['UPLOAD_FOLDER'] = '/images'
I’m developing web page using Flask in github workspace. I want to make it go into my images directory when a user uploads a picture from the web. But I always get an error message saying that file_path is not found in file.save.
I tried to give the address of the directory itself through os.abspath(), and I tried to pass the file path to file.save() without a file name, but all failed.