phpstuff Posted September 24, 2013 Share Posted September 24, 2013 On pg 347 in the 4th TIPS from the top it is mentioned that the move_uploaded_file function will overwrite an existing file with the same name. Questions: 1. How does this work with uploading to folders VS. uploading to database? 2. In a database would it not matter as long as it is a single file being added to a unique user_id? 3. What would you do if there are multiple files and you want to prevent this from happening in a database? In a folder? I'm guessing that you somehow concatenate a random # to the filename? Not sure but I'd sure love to have more insight here as this is something that I plan on implementing in future sites (user can upload 10 photos to a single page, etc). Thanks! Link to comment Share on other sites More sharing options...
Larry Posted September 25, 2013 Share Posted September 25, 2013 I'm really not sure what you mean by "uploading to a database". The move_uploaded_file() function only moves files around within the file system. It can't touch the database. Link to comment Share on other sites More sharing options...
phpstuff Posted September 25, 2013 Author Share Posted September 25, 2013 For example, if you insert files into a DB and the files have the same name, would it not matter because the index is unique? Say there are no records in the database and only Row 1 has a file uploaded: Row 1 data_id photo_of_the_desert.jpg Then somebody else (or that same person) adds another photo and it just so happens to have that same exact file name: Row 2 data_id photo_of_the_desert.jpg I'm assuming each row would have its own unique identifier (data_id might increase incrementally/change for each uploaded file?). I was more or less just curious how uploading files to folders compares to using a DB for storage - advantages, disadvantages, and when you should use one or the other. Link to comment Share on other sites More sharing options...
Larry Posted September 25, 2013 Share Posted September 25, 2013 Okay, to be clear here, you're not inserting files into the database. You're inserting records about files. Although it may seem like semantics, there's a big difference. If you have a UNIQUE restriction on the file name column in the database, then no two users could upload a file with the same name. That's why I wouldn't place a UNIQUE restriction on the file name column in the database. I often store the file itself--the actual file data--on the file system, using a unique name. I store information about the file, including its original name, in the database. You can store files in a database (store the actual file data there), but that's not often a good solution. Link to comment Share on other sites More sharing options...
phpstuff Posted September 26, 2013 Author Share Posted September 26, 2013 Perfect. Thank you for clearing that up. That does make sense and gives me a good starting point to research further. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts