magorich
magorich

Reputation: 459

What does the "PK¿¿¿" response means in PHP

Hi I'm downloading a file to an app on iOS using the function readfile() on a PHP web service and I want to know if the file is downloaded correctly but I don't know how I can do that.

So what I'm trying is to do some echo to know if the file has been downloaded like this:

echo "before";
readfile($file);
echo "after";

But the response I get is this:

beforePK¿¿¿

Any one knows what does this mean or how can I know if the file is downloaded correctly?

UPDATE: Yes it's a zip file, here are my headers

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$ticket");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");

Upvotes: 1

Views: 1379

Answers (3)

Sammitch
Sammitch

Reputation: 32272

Usually you just assume that once the connection has closed that the data is done being transferred. If you want to validate that the file has been transferred fully, and without corruption you'll need to use a data structure like XML or JSON which will:

  1. Delimit the data fields and cause the XML/JSON parser to throw an error if one is omitted, aka the transfer was cut off before it finished.
  2. Allow you to embed more than one piece of data with the response, eg. an MD5 hash of the file that can be re-calculated client-side to verify that the data is intact.

eg:

$file = 'myfile.zip';

$my_data = array(
  'file' => base64_encode(file_get_contents($file)),
  'hash' => md5_file($file)
)

//header calls
header(...)

echo json_encode($my_data);
exit;

Upvotes: 0

Mr. Llama
Mr. Llama

Reputation: 20899

You're trying to output the contents of a zip file aren't you?

readfile($file) works the same as echo file_get_contents($file). If you're trying to present someone a file to download, do not add any additional output else you risk breaking the file.

I would also recommend reading up on the header function. That way you can explicitly tell the browser that you're sending a file, not an HTML page that has file-like contents. (See the examples involving Content-Type)

Upvotes: 1

Rob W
Rob W

Reputation: 9142

PHP should be setting the correct headers prior to readfile() - this LITERALLY reads the file out to the browser/app... but the browser/app needs to know what to do with it...

Upvotes: 0

Related Questions