Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax函数始终返回false后在php中创建file.txt_Php_Ajax_File_Fopen - Fatal编程技术网

ajax函数始终返回false后在php中创建file.txt

ajax函数始终返回false后在php中创建file.txt,php,ajax,file,fopen,Php,Ajax,File,Fopen,我想创建新的.txt文件,但由于此代码总是返回false,因此不会执行ajax成功函数 所有代码为: <?php $nome = $_POST["nome"]; $datanasc = $_POST["datanasc"]; $genero = $_POST["genero"]; $nat = $_POST["nat"]; $morada = $_POST["morada"]; $mail = $_POST["mail"]; $existe = false; $myFile = "Use

我想创建新的.txt文件,但由于此代码总是返回false,因此不会执行ajax成功函数

所有代码为:

<?php

$nome = $_POST["nome"];
$datanasc = $_POST["datanasc"];
$genero = $_POST["genero"];
$nat = $_POST["nat"];
$morada = $_POST["morada"];
$mail = $_POST["mail"];
$existe = false;
$myFile = "Users.txt";
$myFile1 = "Current_User.txt";



$fh = fopen($myFile, "r")or die("can't open file");
while (($line_of_text = fgets($fh))) {
$Data = explode(';', $line_of_text);
    if($nome == $Data[0] && $datanasc == $Data[1] && $genero == $Data[2] && $nat == $Data[3] && $morada == $Data[4] && $mail == $Data[5]){
        $existe = true;
        break;
    }
}

fclose($fh);
if($existe == true){
$arrayToJs["existe"] = $existe;

}

    else{
        $arrayToJs["existe"] = $existe;

        $fh = fopen($myFile, "a")or die("can't open file");
        $stringData = $nome.";".$datanasc.";".$genero.";".$nat.";".$morada.";".$mail.";"."\n";
        //print_r($stringData);
        fwrite($fh, $stringData);
        fclose($fh);

        $fh1 = fopen($myFile1, "w")or die("can't open file");
        fwrite($fh1, $stringData);
        fclose($fh1);


谢谢大家

是否调用AJAX成功函数与PHP代码的“返回值”无关

假设您使用的是jQuery或其他JavaScript框架,那么它与HTTP响应代码有关。您可能会遇到一个PHP错误,导致返回浏览器的响应为500。这将使您最终进入错误处理程序,而不是成功处理程序


您是否尝试过使用Chrome中的network inspector(或Firebug中的Net选项卡)之类的工具来调查实际的HTTP响应?

此代码没有返回任何值。请在将文件创建到ajax响应后传递值(真/假)。

如果您传递到文件的相对路径存在,除非路径恰好与当前PHP目录相关,否则它将返回false

使用使用fclose创建的文件指针($handle):

if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
    $ffav = $nome.'_Favoritos.txt';
    $handle = fopen($ffav, 'w') or die('Cannot open file:  '); 
    fclose($handle);

    $fcart = $nome.'_Cesto.txt';
    $handle = fopen($fcart, 'w') or die('Cannot open file:  '); 
    fclose($handle);
}

否则,当这些文件不存在时,您的文件将始终返回PHP错误

是的,我使用了network inspector chrome,PHP代码没有错误。谢谢Colin文件在同一个目录中。。谢谢这就是问题所在!非常感谢Syamil MJ!
    }

echo json_encode($arrayToJs);
?>
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
    $ffav = $nome.'_Favoritos.txt';
    $handle = fopen($ffav, 'w') or die('Cannot open file:  '); 
    fclose($handle);

    $fcart = $nome.'_Cesto.txt';
    $handle = fopen($fcart, 'w') or die('Cannot open file:  '); 
    fclose($handle);
}