What are best practices for uploading files using PHP to ensure that a database is also updated?
Current approach
1) Form validation confirms all fields are of valid types, lengths and clean of xss attacks.
2) File uploaded to server with user submitted filename
3) Database updated with user submitted details including filename
Is that a safe way? Would it be better to update the database first, then query the database for the filename and then upload the file using the database-retrieved filename?
Not looking for code necessarily but rather the philosophy. Thanks!
I would use this order:
First method: operation order dependent
I strongly suggest using some
PDO-like library to “talk” to database as these throw exceptions, instead of raising errors (likemysql_**function). That way you will know for sure if your query succeeded without constantly checking result of database functions…Second method: Transactions