php代码,用JSON格式的文本文件中的输入类型=“文件”从表单添加数据

时间:2014-08-21 12:22:57

标签: php html

    // path and name of the file
$filetxt = 'http://alkha.com/codecall/formdata.txt';

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

    );

    // path and name of the file
    $filetxt = 'http://alkha.com/codecall/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[] = $data;

    // 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 "data.txt" (in "dirdata" folder)
    // outputs error message if data cannot be saved
    if(file_put_contents($filetxt, $jsondata)) echo 'Data successfully saved';
    else echo 'Tidak dapat menyimpan data di "dirdata/data.txt"';
  }
}
    else echo 'Form fields not submited';
?>     

我上传图片并将文字放在文本字段中但是此消息显示"表单字段未提交",我想用json在文本文件中写入图像路径,但我认为其中的值输入类型="文件"字段消失

0 个答案:

没有答案