Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:02:57+00:00 2026-06-01T14:02:57+00:00

What are best practices for uploading files using PHP to ensure that a database

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:02:58+00:00Added an answer on June 1, 2026 at 2:02 pm

    I would use this order:

    First method: operation order dependent

    • Form validation
    • Check if file was uploaded (don’t move it from temp, yet)
    • Generate new filename based on your free choice
    • Insert data about the file to database using newly generated filename
    • Only then, if there’s not exception thrown, move the file from temp using filename above. Otherwise, file will be deleted automatically on request end
    • If file moving failed for some reason you should remove record from database.

    I strongly suggest using some PDO-like library to “talk” to database as these throw exceptions, instead of raising errors (like mysql_** function). That way you will know for sure if your query succeeded without constantly checking result of database functions…

    Second method: Transactions

    • Form validation
    • Check if file was uploaded (don’t move it from temp, yet)
    • Generate new filename based on your free choice
    • Start transaction
    • “Insert” data about the file to database using newly generated filename.
    • Move the file from temp using filename above. If for some reason file move fails, throw an execption.
    • Commit transaction
    • At this point you’re sure that both file is moved and data persisted properly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that best practices have been followed when designing a new database, how does
What are the best practices for using use cases to model system behavior that
Yahoo best practices states that putting JavaScript files on bottom might make your pages
What are the best practices around creating flat file database structures in PHP? A
Best practices or tools for installing a SQL Server database I have a SQL
What best practices have you used in unit testing embedded software that are peculiar
What's the best practices way to test that a model is valid in rails?
Here's the class I am working on. After uploading a file using a form,
Possible Duplicate: Best practices in error reporting (Mathematica) Assume that I have a function
Just a question about best-practices when upgrading an existing database. Assuming there will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.