我正在尝试将我上传的图像添加到json文件中,但问题是在json文件中查看时图像路径始终返回null。请帮助。谢谢。这是代码,它已经成功追加了,但是最重要的是图像总是返回null。我想将图像显示到我的json文件中,不再返回null。
PHP
<?php
$message = '';
$error = '';
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error = "<label class='text-danger'>Enter Name</label>";
}
else if(empty($_POST["gender"]))
{
$error = "<label class='text-danger'>Enter Gender</label>";
}
else if(empty($_POST["designation"]))
{
$error = "<label class='text-danger'>Enter Designation</label>";
}
else
{
if(file_exists('employee_data.json'))
{
$current_data = file_get_contents('employee_data.json');
$array_data = json_decode($current_data, true);
$extra = array(
'name' => $_POST['name'],
'gender' => $_POST["gender"],
'designation' => $_POST["designation"] ,
'image' => $_POST["output"]
);
$array_data['data'][] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('employee_data.json', $final_data))
{
$message = "<label class='text-success'>File Appended Success fully</p>";
}
}
else
{
$error = 'JSON File not exits';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container" style="width:500px;">
<h3 align="">Append Data to JSON File</h3><br />
<form method="post">
<?php
if(isset($error))
{
echo $error;
}
?>
<br />
<label>Name</label>
<input type="text" name="name" class="form-control" /><br />
<label>Gender</label>
<input type="text" name="gender" class="form-control" /><br />
<label>Designation</label>
<input type="text" name="designation" class="form-control" /><br />
<input id="inputFileToLoad" type="file" onchange="encodeImageFileAsURL();" />
<hr>
<h2>Output</h2>
<div class="output" name="output">
<textarea class="form-control"></textarea><br>
<a name="new"></a><br><br>
<img><br>
</div>
<input type="submit" name="submit" value="Append" class="btn btn-info" /><br />
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
</div>
<br />
</body>
</html>
<script type="text/javascript">
function encodeImageFileAsURL(cb) {
return function(){
var file = this.files[0];
var reader = new FileReader();
reader.onloadend = function () {
cb(reader.result);
}
reader.readAsDataURL(file);
}
}
$('#inputFileToLoad').change(encodeImageFileAsURL(function(base64Img){
$('.output')
.find('textarea')
.val(base64Img)
.end()
.find('a')
.attr('href', base64Img)
.text(base64Img)
.end()
.find('img')
.attr('src', base64Img);
}));
</script>
employee_data.json
{"data":[{"name":"sdfghj","gender":"as","designation":"asdfg","image":null}]}