Storing PHP(/PHP-FPM/Apache)'s temporary-from-upload files in RAM rather than the filesystem (or encrypted only)? <h2>Original question</h2> <p>So the project I'm working on is deathly paranoid about file uploads.<br> In the scope of this question, I'm not using that term in regards to payloads; I'm talking <em>confidentiality</em>.</p> <p>Programs can always crash and leave temporary files loafing around in the filesystem. That's normal. The <em>slightly</em> confidentiality-paranoid can write a cronjob that hits the temporary file folder every few minutes and deletes anything older than a few seconds prior to the cronjob call (not <em>everything</em>, simply because otherwise it might catch a file in process of being uploaded).</p> <p>...unfortunately, we take this paranoid a step further:</p> <p>Ideally, we'd love to never see temporary files from file uploads anywhere but in process-associated RAM.</p> <p><strong>Is there a way to teach PHP to look for temporary file as blobs in memory rather than in the filesystem?</strong> We use PHP-FPM as a CGI handler and Apache as our webserver, in case that makes it any easier. (Note also: 'Filesystem' is the keyword here, rather than 'disc', since there are of course ways to map the filesystem to RAM, but that doesn't fix the accessibility and automatic post-crash-clean-up issue.)</p> <p><strong>Alternatively, is there a way these temporary files can be <em>encrypted</em> immediately when they're being written to disc</strong>, so that they're never held in the file system without encryption?</p> <hr> <h2>Thread overview</h2> <p>I can unfortunately only accept <em>one</em> answer - but to anyone reading this, the entire thread is <em>extremely</em> valuable and contains the collective insights of many people. Depending on what <em>you</em> are hoping to achieve, the accepted answer <em>may not be interesting to you</em>. If you've come here through a search engine, <strong>please take a moment to read the whole thread</strong>.</p> <p>Here is a compilation of usecases as I see them for quick reference:</p> <h3>Re: PHP's temporary files</h3> <ul> <li><p>RAM instead of disc (e.g. due to I/O concerns) &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5702449#5702449">RAMdisk</a>/comparable (<em>plasmid87</em>, <em>Joe Hopfgartner</em>)</p></li> <li><p>Immediate (per-filesystem-user) encryption &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5733277#5733277">encFS</a> (<em>ADW</em>) (+ <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5879171#5879171">a gotcha</a> as per <em>Sander Marechal</em>)</p></li> <li><p>Secure file permissions &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5905221#5905221">restrictive native Linux permissions (optionally per <strong>vhost</strong>)</a> (<em>Gilles</em>) or SELinux (see various comments)</p></li> <li><p><strong>Process-attached memory instead of filesystem (so a process crash removes the files)</strong> (originally intended by the question)</p> <ul> <li><p>don't let the file data reach PHP directly &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5878263#5878263">reverse-proxy</a> (<em>Cal</em>)</p></li> <li><p>disable PHP writing to the filesystem &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5867691#5867691">see PHP bug link in this answer</a> (<em>Stephan B</em>) or <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5877346#5877346">run PHP in CGI mode</a> (<em>Phil Lello</em>)</p></li> <li><p>write-only files &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5904618#5904618"><code>/dev/null</code> filesystem</a> (<em>Phil Lello</em>) (this is useful if you have access to the data as a stream <em>additionally</em> but cannot turn off the file-writing functionality that runs in parallel; whether PHP allows this is unclear)</p></li> </ul></li> </ul> <h3>Re: your files, post-upload</h3> <ul> <li>storing in database instead of disc &rarr; <a href="http://stackoverflow.com/questions/5701508/storing-php-php-fpm-apaches-temporary-from-upload-files-in-ram-rather-than-the/5862044#5862044">file encryption in a database HowTo</a> (<em>Rook</em>)</li> </ul>