Solve PHP Server Disk Storage Over Quote Problem

On December 16, 2015, in Other Online Technology, by James Liu

After I release a free web server, my PHP server is suffering from resource exhausting problem. The main problem is my server disk storage over quote. The problem is too serious because after the disk is full, I cannot login the cPanel. After that, all my website is down. As I am saving session in the file. After the disk is full, the login function doesn’t work any more because session cannot create new file to save the data.

The only solution for this situation is updating my server or finding a way to keep disk having enough space. Because of the budget issue, I don’t have enough money to upgrade server. So the only way to solve problem is keeping the disk having enough space all the time.

Manually Delete Temporary File Regularly

At the beginning, I just use FTP client to connect my server and delete all the temporary file manually. After the usage increasing, my server disk is getting full very quickly. Therefore, this solution is not suitable now.

Trigger PHP Script To Delete Temporary File

Then I try to figure out a new solution. I create a PHP script in my server, which will delete the temporary file. This is much easier than using FTP to do so. Here is the PHP script source code.

function deleteAllFile($folder) {
	$files = scandir($folder);
	foreach( $files as $file) {
		if($file != "." && $file != ".." && $file != "index.html") {
			$filePath = $folder."/".$file;
			if(is_dir($filePath)) {
				deleteAllFile($filePath);
				rmdir($filePath);
			} else {
				//echo $filePath."<br/>";
				unlink($filePath);
			}
		}
	}
}

The above script will delete all files and folders. I just need to know the PHP script url on my website. I can trigger this script at any time. This solution is much better than the first one. However, it is still not the perfect solution. There is a security issue.

Use cPanel Cron Jobs

If you are using cPanel or you are familiar with Linux, you will know Cron job. It is a schedule task which can help you run a script or Linux command on time. This makes the maintenance very simple now. Here is my shell script which help me delete temporary files in two folders, but keep the index.html file in both folders.

rm -rf ./folder1/*.*
rm -rf ./folder2/*.*

cp ./index.html ./folder1/index.html
cp ./index.html ./folder2/index.html

In cPanel, it will send an email to you once the task is executed. You can find all the output information of your task in that email. In this way, I don’t need to manually delete temporary files or run PHP script to save my disk space any more.

 

Leave a Reply

Free WordPress Theme

Premium WordPress Themes