What are the advantages of using a randomly generated file name for uploaded files and storing the original file name in a database?
Using a randomly generated file name for uploaded files adds an extra layer of security by making it harder for malicious users to access sensitive information through predictable file names. Storing the original file name in a database allows for easy retrieval and display to users without compromising security.
// Generate a random file name for uploaded files
$randomFileName = uniqid() . '.' . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// Store the original file name in a database
$originalFileName = $_FILES['file']['name'];
// Insert $originalFileName into your database along with other file details
// Move the uploaded file to a secure directory with the random file name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $randomFileName);
Related Questions
- Are there any pitfalls to be aware of when downloading PHP tutorials for offline use?
- What are some potential pitfalls of storing dates as VARCHAR in a database when using PHP?
- How does accessing PHP scripts through a local editor using file:// differ from accessing them through a web server using http://?