This answer assumes you mean execute when you say view.
First off, the safest way to prevent php from being executed is to actually turn it off. As suggest in a previous answer, php_flag engine off
is one way of doing that, but as far as I know that may not work, if PHP is invoked via fastcgi. You can use the RemoveHandler
Directive, but you have to make sure, you catch all file extensions for which php has been defined, this is not necessarily limited to *.php.
This answer suggest, you should put the following code in your .htaccess:
<Files *>
SetHandler default-handler
</Files>
But there's still one problem: If you allow people to upload any file, they can upload their own .htaccess and overwrite whatever you have put in yours.
If you are the server admin, there are 2 possible solutions for that: You can disable php altogether by not including the module at all, but that means you'd have no php server-wide and that may not be what you want. Or you could use AllowOverride None
to disable .htaccess files for your upload folder, but I think that only works in the server config and not in another .htaccess so you could put this in your httpd.conf:
<Directory /path/to/your/upload/folder>
#To ignore uploaded .htaccess:
AllowOverride None
<Files *>
SetHandler default-handler
</Files>
</Directory>
Of course you have to change the path. The answer I linked above suggests an even more extensive approach, maybe you should have a look that too. (I know links are difficult to see here, so here's the full url, just in case: Disable all CGI (php, perl, …) for a directory using .htaccess )
So, if you're not the server admin, you're probably safest if you either disallow certain file types, file names or rename every uploaded file with a chosen prefix or suffix, that you remove in a script or with mod_rewrite before a user downloads the file again.