Json不存储输入文件的值

  • 本文关键字:文件 存储 Json php html json
  • 更新时间 :
  • 英文 :


你好,我最近创建了一个这样的表单:

<form method="post" Action="Save.php" enctype="multipart/form-data">
<label>Nome:</label><br><br>
<input name="Nome" ><br><br>
<label>Imagem:</label><br><br>
<div class='input-wrapper'>
<label for='input-file'>
Select a file
</label>
<input type="File" name="img" id="input-file"><br></div><br><label>Mensagem:</label><br><br><textarea name="msg"></textarea>
<br><br>
<input type="submit" class="btn_post">
</form>

而不是save.php看起来像这样:

<?php
// Here is more debug information:      
$values = json_encode($_POST);
// Stores received values ​​at the end of the file.
file_put_contents('save.json', $values, FILE_APPEND);

发送表单时,它在save。json中是这样的:

{"Nome":"Nicolas","img":"aef9bd64-6990-4f08-8238-2baf005f42f7.jpg","msg":"Incredible"}

到目前为止都很好因为我得到了用户在输入文件中发送的文件名,但我没有收到文件(图像),所以我把这段代码放在** Save.php **,看起来像这样:

<?php
//
// Take the post request and transform it into JSON.
$values = json_encode($_POST);
// Stores received values ​​at the end of the file.
file_put_contents('save.json', $values, FILE_APPEND);
$uploaddir = 'img/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
echo "Valid file and sent successfully. N";
} else {
echo "Possible file upload attack!  N";
}
echo 'Here is more debug information:';
print_r($_FILES);
print "</pre>";

现在我在$ uploaddr中声明的文件夹中获得了文件加上保存。Json是这样的:

{"Nome":"Nicolas","msg":"Incredible"}

也就是说,我不再接收输入文件的值有什么问题?

未测试,但您可以做类似的事情,也许:

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_FILES ) ){

# create the json variable and add attributes of the $_FILES object to the output
$values=json_encode( $_POST );
$values->filename=$_FILES['img']['name'];
$values->size=$_FILES['img']['size'];

# do the upload processing as previously
$uploaddir = 'img/';
$uploadfile = $uploaddir . basename( $_FILES['img']['name'] );
#move the file as per previous
if( move_uploaded_file( $_FILES['img']['tmp_name'], $uploadfile ) ) {
$status="Arquivo válido e enviado com sucesso.";
} else {
$status="Possível ataque de upload de arquivo!";
}   


#store status of upload 
$values->status=$status;
$values->savepath=$uploadfile;

# store with more info
file_put_contents('save.json', $values, FILE_APPEND );

}

相关内容

最新更新