user3104316
user3104316

Reputation: 45

PHP: Checking file size whilst downloading file to server

The user is to provide a URL from which the server will fetch a file using file_put_contents()

I would like to restrict the file size to 1MB

I am able to check the file size of some files prior to downloading using THIS solution. However not every HEAD request returns the size of a file prior to its download.

Is there a way to stop the download of a file to a server if the file size exceeds a certain number of bites?

Upvotes: 0

Views: 84

Answers (2)

user3104316
user3104316

Reputation: 45

Answer as suggested by Sergiu Paraschiv comments:

function get_file_Name_from_URL( $url, $SizeLimit ) {
$_1MB = 8000000;
$Limit = $_1MB *  $SizeLimit;
$file = fopen("$url","r");
fread($file,"$Limit");

// check for end of file
if (feof($file)){




return true;
} else{

//File is too big


return false;
}

fclose($file);
}

Upvotes: 1

Wojciech S.
Wojciech S.

Reputation: 16

I'm not sure if you can check transfer during upload.

Instead you can check the filesize after you download it and if it is more than 1MB, delete it.

Upvotes: 0

Related Questions