Display image from tmp directory

I have the following. A website that creates temporary images in the / tmp folder on a Linux server. The reason I keep it in this folder is because these images need to be cleaned up once in a while, and it's much easier to just clean up the tmp directory with tmpwatch. Now my problem is displaying the image in my browser?

the code

<img src="/tmp/3d34636.png" alt="image" />  

      

I am running Centos with PHP

+2


a source to share


3 answers


The problem is that this img tag will be processed by the browser (client). This client does not have access to the HTTP server file system.

Therefore, you need to define the / tmp directory as a valid location for your HTTP server using files in the / tmp / directory (but note that it will serve any other file contained in that directory, potentially a safe risk).



Or, you store these generated images in a subdirectory that is already served by your HTTP server.

It is also possible to define this / tmp location on the server to be served by a script that will parse a URL containing the name of the image file. This script will then open the file in / tmp / and serve it up.

+1


a source


@Didier already describes the issue and security risks. Even if you created a PHP script that takes a file name as a parameter and then steps through the file from the directory /tmp

, you will be mixing public content with temporary files that might contain sensitive data. You would need to keep a list of the files your script generated and which can be displayed and which not, otherwise it would be a security hole.



I would say forgot /tmp

for this despite the benefits and saved your images in their own subdirectory. Remove them frequently (for example, using a cron job or based on the age of the file or the time it was last accessed).

+2


a source


You can get tmpwatch to clean up another directory by creating a cron job that runs the command:

/usr/sbin/tmpwatch -umc 240 /path/to/directory

      

240

- the number of hours during which the files were not used before deletion (in this case, 10 days). For details see man tmpwatch

.

+1


a source







All Articles