3

我正在尝试下载由 php 页面输出的 Android APK 文件。下载成功,但是尝试安装apk文件时出现解析包问题。实际上,直接访问文件路径(/var/www/xxx/HelloWorldTest.apk),这个方法完全安装。(没问题。)但我想使用通过php源的方法。

我的服务器上有 Android 应用程序,也有 php 代码,例如:

<?php
$path = "/var/www/xxx/";
$filename="HelloWorldTest.apk";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/vnd.android.package-archive");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path . $filename));
readfile($path . $filename);
?>

有 HelloWorldTest(Android app) 清单,如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworldtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworldtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的设置:CentOS release 6.3 (Final),2.6.32-279.5.2.el6.x86_64, PHP 5.3.17 Android table Version:3.2.1 kernel version:2.6.36.3

使用直接访问方法和使用 php 方法结果的二进制转储存在差异。

直接访问(成功)

50 4B 03 04 14 00 08 08  08 00 F5 91 76 42 00 00 
00 00 00 00 00 00 00 00  00 00 1C 00 04 00 72 65 
73 2F 6C 61 79 6F 75 74  2F 61 63 74 69 76 69 74 
79 5F 6D 61 69 6E 2E 78  6D 6C FE CA 00 00 6D 91
...

通过 php(安装失败)

0A 0A 50 4B 03 04 14 00  08 08 08 00 F5 91 76 42 
00 00 00 00 00 00 00 00  00 00 00 00 1C 00 04 00 
72 65 73 2F 6C 61 79 6F  75 74 2F 61 63 74 69 76 
69 74 79 5F 6D 61 69 6E  2E 78 6D 6C FE CA 00 00
.....

问题似乎是在二进制代码中添加了初始部分 [0A 0A]。

4

1 回答 1

3

0A 是换行的十六进制代码。因此,您似乎在文件的实际内容之前添加了两个换行符。通过阅读readfile上的文档,建议使用以下内容围绕 readfile 调用:

ob_clean();
flush();
readfile($path . $filename);
exit;

试试看你是否还有两个 0A 字符。

于 2013-04-26T12:13:13.993 回答