Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use fwrite() or move_uploaded_file()?

It's more of a coding standards question this one. Which one is, if I can call it "better" to use in file upload handler scripts?

I know that fwrite() and it's accompanying methods for reading and writing can do it in chunks but using move_uploaded_file() is much more elegant and shorter code.

Thanks

like image 417
Etienne Marais Avatar asked Dec 06 '22 23:12

Etienne Marais


1 Answers

Use move_uploaded_file(). It does extra checks to ensure the user is not into any funny business. Also, using fread() and fwrite() copies the file, instead of moving it, which is a few orders of magnitude more costly than just moving it (which basically just changes it's name, given that the source and the destination are on the same partition).

like image 51
Felix Avatar answered Dec 10 '22 12:12

Felix