How can I compress(using zip) a file when it exceeds certain limit on server?

My question is about compressing a file. I am using PHP in my website. I am collecting user comments on a file on the server. I have already restricted the message size, but for just to be prevent possible misuse, I want to do that; if the file’s size is greater than some limit(such as 5Mb) then I want to compress the file and delete the old one. Here, my question branches into two sub-topics:

  1. Can I do it automatically for a file on the server side? Or do I have to do it manually(i.e downloading the file and delete it)? How can I do it?
  2. In PHP: Installation - Manual ,I saw a statement such that I think server must have a module to do that. Does infinityfree support any file compression support feature on servers that I can use for this purpose? If so how?
    Thanks :slight_smile:

We don’t recommend that you allow users to upload files to your website. If a user uploads something that is in violation of our terms (Adult or illegal content for example), you will be permanently suspended and you will be held fully responsable for any and all media and content found on your account.

3 Likes

:slight_smile: Greenreader9, I do not allow users to upload any file, just leave a comments over a contact form inside my website. I just store those text comments in a file.

What I talked about is this : http://enginery.freecluster.eu/index.php?page=about
Simple contact page, users can only enters texts I think in this form?

Can they upload any other content/file/etc using my this form? If so I should close that form !

I prepared below code for cron jobs and to see whether my cron job/script works :

<?php
$zip = new ZipArchive;

$filename = './path/to/file/star.txt';
if ( filesize($filename) > (5*1024) ) {
	$file = fopen('./path/to/check.txt', 'a'); //opens file in append mode  
	$result="";
	if ($zip->open('./path/to/zippedFile.zip') === TRUE) {
		$zip->addFile($filename);
		$zip->close();
    
		$result='File successfully ompressed at '.date("d/M/Y l H:m:s T");
		$fp = fopen($filename, "w");
		fclose($fp);
	} else {
		$result='Failed to compress comments file at '.date("d/M/Y l H:m:s T") ;
	}
	
	fwrite($file, $result);  
	fclose($file); 
} else {
	// 	to see whether cron is working or not...
	$file = fopen('./path/to/check.txt', 'a');//opens file in append mode  
	$result="File size is less than 5 Mb at ".date("d/M/Y l H:m:s T");
	fwrite($file, $result);  
	fclose($file); 
}

?>

but nothing happens(i.e. no check.txt file created) after 2 minute cron job. What should be the problem?

This is how cron job works here

3 Likes

Thanks KangJL, page is open in my browser too : Understanding Cron Job schedules

However, no result is coming. My set up is 2 minutes and 0 hours.

Hello

Cron jobs are really weird here (Sometimes they work, sometimes not). 1hr 5min seems to be a time that works.

Also, if you run that PHP script every 2 minutes, it will run 720 times a day. Combine that with real users visiting your website, you will probably be hitting some limits.

When so body submits the form, I don’t really recommend that you store the data on the server. Not only are you storing sensitive data (Email, Name, etc), its not really the best for you. The best way to do it is to have the form email responses to you.

That way, you don’t have to worry about storing it or zipping it, and you get notified whenever someone submits the form. Plus, you can use email filters to sort out the spam.

1 Like

Thanks Greenreader9, I actually set up to 2 minutes to try whether cronjob is working or not, so 2 minutes is not ultimate choice of mine.

I read all those options about mailing : How to send email with Gmail SMTP and sample contact form in GitHub - InfinityFreeHosting/contactform: Simple contact form with PHPMailer. However, it requires new gmail account etc. . I will try to solve that “cron job”. Thanks for all efforts to help.

Nope, you can use your existing one! Since it is recommended that you use 2Fa anyways (Especially with everything that is going on), all you have to do is generate an app password and use that GitHub project you linked!

4 Likes

Ok, I changed my above test function for cron job(I can’t make my cron job to work, so I leave it) and put it in the page where the form was. When user enters to that page and submit a comment, my function warns me that on the same page, file content exceeds limit(real 5*1024*1024 = 5Mb limit) (i.e onsubmit). I will manually download and delete the file from the server than. By doing that I saw that, my above test code, that creates a zip file and put the file into it , did not work since “zip” module not installed on the server side which is the main answer of my questions. :neutral_face:

That’s very strange, because the zip extension should be enabled by default on all sites.

Can you please confirm your website is running on PHP 7.4? Older PHP versions may have some functionality disabled due to known security issues.

2 Likes

Thanks for answer! If you did not answer, I will not look and find PHP info page! I looked and found that my website’s PHP version is 7.0, however, when I looked PHPconfig page, it say " Zip Enabled and Zip version 1.15.6 ". My zippipng code was as above :

<?php
$filename = './path/to/file/star.txt';
if ( filesize($filename) > (100) ) { // to test - if greater than 100 bytes
	$file = fopen('./path/to/check.txt', 'a'); //opens file in append mode  
	$result="";
        $zip = new ZipArchive;
	if ($zip->open('./path/to/zippedFile.zip') === TRUE) {
		$zip->addFile($filename,date("dMY_Hms").".txt"); // extension dot was missing, added
		$zip->close();
    
		$result='File successfully compressed at '.date("d/M/Y l H:m:s T");
                // clear the file content - blanking
		$fp = fopen($filename, "w");
		fclose($fp);
	} else {
		$result='Failed to compress comments file at '.date("d/M/Y l H:m:s T") ;
	}
	fwrite($file, $result);  
	fclose($file); 
} 
?>

My php code does not give any error. From now on, I will work on my zipping code more. If I succeed, I will write the final code here. Maybe in future it will help somebody…

I finally managed to run my zipping code. Although I get my above code from PHP’s manual page PHP: ZipArchive::addFile - Manual , code only used for opening an existing file and adding into it, not creating a new zip file. So correct sample code was in PHP: ZipArchive::open - Manual. :no_mouth: That was my mistake. So final working code was ;

<?php
$filename = './path/to/file/star.txt';
if ( filesize($filename) > (100) ) { // to test - if greater than 100 bytes
	$file = fopen('./path/to/check.txt', 'a'); //opens file in append mode  
	$result="";
        $zip = new ZipArchive;
	if ($zip->open('./contacts/'.date("dMY").'.zip', ZipArchive::CREATE) === TRUE) {
		$zip->addFile($filename,date("dMY_Hms").".txt"); // extension dot was missing, added
		$zip->close();
    
		$result='File successfully compressed at '.date("d/M/Y l H:m:s T");
                // clear the file content - blanking
		$fp = fopen($filename, "w");
		fclose($fp);
	} else {
		$result='Failed to compress comments file at '.date("d/M/Y l H:m:s T") ;
	}
	fwrite($file, $result);  
	fclose($file); 
} 
?>

It worked, and I got a zipped file. Thanks dear Admin, have a nice and healty days. :pray:

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.