【发布时间】:2018-11-14 09:10:09
【问题描述】:
我正在尝试为$filename 数组中的每个元素写入文件。
错误:
注意:数组到字符串的转换 C:\xampp\htdocs\insertSalestest.php 第 22 行
注意:数组到字符串的转换 C:\xampp\htdocs\insertSalestest.php 第 22 行
注意:数组到字符串的转换 C:\xampp\htdocs\insertSalestest.php 第 22 行
注意:数组到字符串的转换 C:\xampp\htdocs\insertSalestest.php 第 22 行 {"state":105}
PHP 代码:
<?php
include "connection.php.php";
$sucess = 105;
$fail = 107;
$result;
$base = array("my", "litte", "array", 2);
$filename = array("my", "litte", "array", 2);
foreach($base as $option) {
$sql = "INSERT INTO sale_image(sale_id, img_url) VALUES ('1','$option');";
$result = mysqli_query($con, $sql);
}
foreach($filename as $value) {
$imageNow = time();
$new_imageFilename = $imageNow."_".$filename;
$binary=base64_decode($value);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('files/'.$new_imageFilename, 'wb');
fwrite($file, $binary);
fclose($file);
}
$jsonResult = '{"state":';
if($result){
$jsonResult.= $sucess;
}else {
$jsonResult.= $fail;
}
$jsonResult .="}";
print_r($jsonResult);
?>
【问题讨论】:
-
我认为错误来自这里:
$new_imageFilename = $imageNow."_".$filename;You are concatenating an array to a string -
您在声明循环时将
$filename视为数组,然后将其视为其中的字符串。我很确定您第二次是指$value。 -
你能解释一下这个
header('Content-Type: bitmap; charset=utf-8');请... -
你能帮我具体编辑什么吗?,因为我试图多次编辑代码,而且每次都是一样的,它只写一次文件
-
@marvinIsSacul 我只是在寻找如何将字符串写为图像,我得到了这段代码,我只是 php 的新手,我有点困惑。