Check if download is interupted and check filesize OS independent

I build php based download/upload remote upload/reupload/download server using curl. I manage almost everything. Working with http, https, ftp and ftps protocols.  Script download any file from remote server and output it as force/download, inline etc. Now I adding option if you download some file from remote server to make local copy on server where is script hosted. That work also.

What I can not solve for now is:

1. Is there any workaround to detect when user stop download. EG. Script support resume download, rage, multi range and chunked downloads.

- I do it with time difference and packet output, but I do not like it, hight load server and must check every loop because I use CURLOPT_WRITEFUNCTION and callback.

- Also there are if(connection_aborted() || connection_status() == 0) , but not working with Chrome. In Firefox is working with too long timeout.

2. Other thing is I want to delete file if download is incomplete. For original filesize I use curl_$this->getStatus = getinfo($curl); and for local copy filesize()

	if(filesize($this->local_copy.(rawurldecode($this->fileName))) !== $this->getStatus['download_content_length']){
		
		unlink($this->local_copy.(rawurldecode($this->fileName)));
	}

Open in new window



But every time file is unlinked no matter of real file size limits for files >2GB with php filesize function.

Thanks in advance!!!
LVL 4
te-eduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

te-eduAuthor Commented:
OK I have solution for file size so I will share. This is common asked question over internet but as I see without too much exact solutions.

1. you must add ignore_user_abort(true); to script continue execution if user press cancel.
2. you must check connection status with some loop connection_status():
     0-ok
     1-closed
     2- timeout

First answer to mine 1th question. You must add ignore_user_abort(true); before you init curl session. Without it you can not detect if user aborts download. Simple and working. If user pause download curl session will not finish. In mine case question 2 I need to delete local file copy if user abort connection. This script download from remote location and serve force download. I do like this curl !!!

As I use fwrite to create local file copy, incomplete downloaded file didn't wont to be deleted because I didn't close file after write.
fclose($this->wFile); 
$localCopySize = filesize($this->local_copy.(rawurldecode($this->fileName))); 
$checkDifference = (int)$this->getStatus['download_content_length'] - (int)$localCopySize; 
	if($checkDifference > 0){
		unlink($this->local_copy.(rawurldecode($this->fileName)));
	}

Open in new window


Nice during mz posting I get new idea why I should delete file?? I could leave curl to download local copy even if user abort download and if he try new download with same file I will serve just local file copy.

Even no one answer this question I get some use from it NEW IDEAS!!!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
te-eduAuthor Commented:
I find myself solution for this problem, also I publish solution to help others.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.