HOW TO STORE A FILE AND MAKE IT DOWNLOADABLE?
I often come across this question and new comers often find it difficult to understand. Let me put this thing in simple way.
What do we need to do this?
- Store file at some permanent location
- Store data about file
- Serve the file whenever user request for it.
[php] //! Generate a unique file name $uniqueFileName == md5(uniqid('',true)); //! Generate a upload path $uploadfileTo = UPLOAD_PATH. $uniqueFileName ; //! Move to permanent location move_uploaded_file($file['tmp_name'], $uploadfileTo);<br />[/php]
uniqid() generates a random string. First argument is prefix for the random string. If we pass second argument as true, a string with more entropy will be generated , i.e. more random numbers. We are hashing the generated string using md5 algorithm. You can also use the hash of combination of timestamp and output of uniqid().
Now if we have store the file, we will need information of file in our database. I would recommend storing at least this details about file:
- File’s original name
- File’s extension
- File’s Content-Type / MIME type
- File Size
- Storage location
- File’s generated name
- Redirect user to the URL of actual stored file.
- Create a proxy page which will serve the content of file.
[php] //! pass content type header for the file header("Content-type: {$sContentType}"); //! force browser to download the content header("Content-Disposition:attachment;filename='{$sFileName}'"); //! load the content of the file readfile($filePath); [/php]
And that’s it. Your file will be served to user with original name and content type. And user will actually never know the real location.
Common issue people face with this is, file is downloaded properly but when they open it, they realized that the file is corrupted. This happens because you are sending some other output to the browser also as payload along with content of file. You may have some code which is echoingsomething. You can also use output buffering to stop sending anything to browser except the actual content. Use output buffering, before you readfile flush the output buffer and then read the file.