0

我需要从 php 文件中获取字符串有人知道我该怎么做?

My.php 写入和读取一个文件的内容

<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($myFile)); //this is the string that i have to pass
         fclose($fh); 

         return $theData;

 }


 ?>

my.js 脚本,它使用 get 方法从我的 php 文件中检索字符串

 function addLabelCustom_options() {
  var select = document.getElementById('label_custom');

 $.get("JS/foo.php", function(result){ alert(result) }, "json");


 /* $.ajax({
  url:'/var/www/devData/test/ciao.txt',
  success: function (data){
  //parse ur data
  //you can split into lines using data.split('\n') 
  //use regex functions to effectivley parse
  var label_parsed = data.splitCSV();
    select.options[0] = new Option("-- Select Label --",0);
    var label_sort = new Array();

    for (var i=0; i<label_parsed.length-1; i++)
        label_sort.push(label_parsed[i][1]);

    label_sort.sort();
    for (var j=0; j<label_sort.length-1; j++)
        select.options[j+1] = new Option(label_sort[j],j+1);

}
});

*/
}

我只需要获取字符串 $theData 的内容 看起来如果我不检索任何字符串....

4

3 回答 3

1
<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($myFile)); //this is the string that i have to pass
         fclose($fh); 

         echo $theData;  // <-- ECHO

 }


 ?>

您必须输出数据,而不是返回数据。也就是说,呼应它。如果字符串尚未 json 编码,请使用echo json_encode($theData);

于 2012-07-31T09:57:57.193 回答
0

在 php 函数 (echo $theData; ) 中回显内容后,尝试以下 jquery 代码

$.ajax({
 url:JS/foo.php,
 success:function(result){ alert(result) }, "json"),
 error:function(alert('Not Completed'))
};

并检查控制台中的响应以检查是否有任何错误。您可以在firefox中查看F12的控制台

于 2012-07-31T10:15:53.817 回答
0

伙计们,我解决了这个问题,就像每次都是愚蠢的事情,每次我明白我真的很愚蠢,但是我发布了解决方案。

用于创建目录并存储在文件中的第一个 php 文件

<?php
 $dir = '/var/www/devData/test';

 // create new directory with 777 permissions if it does not exist yet
 // owner will be the user/group the PHP script is run under
 if ( !file_exists($dir) ) {
  mkdir ($dir, 0777);
 }
 if (isset($_POST['data'])) {

         $stringData = $_POST['data'];
         $file = "/var/www/devData/test/ciao.txt"; 
         $fh = fopen($file, 'r+') or die("can't open file");
         fwrite($fh, $stringData);
         $theData = fread($fh, filesize($file));
         fclose($fh); 

        echo json_encode($theData);
 }


 ?>

为了阅读内容,我创建了另一个文件 php

<?php


        $file = "/var/www/devData/test/ciao.txt"; 
            $fh = fopen($file, 'r') or die("can't open file");
        $theData = fread($fh, filesize($file));<--- was the error before i used Myfile but that variable wasn't declare and was empty :) lol 
        fclose($fh); 

        echo json_encode($theData);


 ?>

谢谢大家!!

于 2012-07-31T11:22:43.593 回答