user1985273
user1985273

Reputation: 1967

PHP put - file size?

Im trying to find out the size of the file witch came through a put request in php. Currently im uploading my file like this:

<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");

/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");

/* Read the data 1 KB at a time
   and write to the file */
while ($data = fread($putdata, 1024))
  fwrite($fp, $data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>

But if im transfering large files and there is a possibility that the upload may be canceled in the middle i would like to check that the size of the file written is the same as the original size of the file being uploaded. How can that be done?

Also it would be great if someone could explain this part to me a little:

$putdata = fopen("php://input", "r");

In manual it is explained like this: php://input is a read-only stream that allows you to read raw data from the request body.

But i really dont understand this. If a file is being read like this is it read little by little or is the whole thing read at once?

Thank you.

Upvotes: 2

Views: 162

Answers (2)

Krimson
Krimson

Reputation: 7664

hmm, interesting problem. if i were you I would do it this way.

1. on the client side:

using java script generate a md5 hash of the file and send it in your form

2. on the server side use php to generate the md5 hash

Then compare them, if the two hashes are the same, then the file has been successfuly uploaded or else...

Upvotes: 0

Toby Allen
Toby Allen

Reputation: 11213

As far as I am aware there is no way to do this reliably unless you have a client application as well. You may need to do the entire file transfer in javascript.

Upvotes: 3

Related Questions