简体   繁体   中英

unable to save data to text file using php to save in json format

I use this code to insert data to text file but its give unable to save date any help pleas its can post the file .text but no data can be saved Unable to save data in "dirdata/formdata.txt"

save_json.php

<?php
// Append new form data in json string saved in text file
// From: http://coursesweb.net/php-mysql/

// path and name of the file
$filetxt = 'dirdata/formdata.txt';

// check if all form data are submited, else output error message
        if(isset($_POST['category']) && isset($_POST['name']) && isset($_POST['link']) && isset($_POST['pic']) ) {
  // if form fields are empty, outputs message, else, gets their data
        if(empty($_POST['category']) || empty($_POST['name']) || empty($_POST['link']) || empty($_POST['pic'])) {                   
    echo 'All fields are required';
  }
  else {
    // gets and adds form data into an array
    $formdata = array(
      'category'=> (float) $_POST['category'],
          'name'=> (float) $_POST['name'],
          'link'=> (float) $_POST['link'],
          'pic'=> (float) $_POST['pic'],
    );

    // path and name of the file
    $filetxt = 'dirdata/formdata.txt';

    $arr_data = array();        // to store all form data

    // check if the file exists
    if(file_exists($filetxt)) {
      // gets json-data from file
      $jsondata = file_get_contents($filetxt);

      // converts json string into array
      $arr_data = json_decode($jsondata, true);
    }

    // appends the array with new form data
    $arr_data[] = $formdata;

    // encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
    $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

    // saves the json string in "formdata.txt" (in "dirdata" folder)
    // outputs error message if data cannot be saved
    if(file_put_contents('dirdata/formdata.txt',$jsondata)) echo 'Data successfully saved';
    else echo 'Unable to save data in "dirdata/formdata.txt"';
  }
}
else echo 'Form fields not submited';
?>

​

index.html

<form action="save_json.php" method="post"><br><br><br><br>
<head>
<title>Add iptv channel</title>
</head>
<center><body>
 ADD IPTV CHANNEL
</body></center><br><br>

<center>category: <input type="text" name="category" id="category" SIZE="100" MAXLENGTH="40"/></br>.</center><br>

<center>name: <input type="text" name="name" id="name" SIZE="100" MAXLENGTH="40"/></br><br>.</center>

<center>link: <input type="text" name="link" id="link"SIZE="100" MAXLENGTH="40" /></br><br>.</center>

<center>pic: <input type="text" name="pic" id="pic" SIZE="100" MAXLENGTH="40"/></br><br>.</center>

<center><input type="submit" id="submit" value="Submit" /></center>​

file_put_contents需要完整的服务器路径

$filetxt = $_SERVER['DOCUMENT_ROOT'].'dirdata/formdata.txt'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM