【问题标题】:I want to write file for every elemnt in the array using php我想使用 php 为数组中的每个元素编写文件
【发布时间】: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 的新手,我有点困惑。

标签: php mysql arrays


【解决方案1】:

如果您不尝试自己将原始标头发送到浏览器,则无需使用header('Content-Type: bitmap; charset=utf-8');

无论如何我想我明白你想要什么所以试试这个......

<?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 . "_" . $value; // use $value instead of $filename - because $filename is an array
        $binary=base64_decode($value);
            /* header('Content-Type: bitmap; charset=utf-8'); */ // don't see the need
        $file = fopen('files/' . $new_imageFilename, 'wb');
        fwrite($file, $binary);
        fclose($file);      
    }

    $jsonResult = '{"state":';
    if($result){
        $jsonResult.= $sucess;
    }else {
        $jsonResult.= $fail;
    }
    $jsonResult .="}";
    print_r($jsonResult);

【讨论】:

  • 非常感谢,你拯救了我的一天。
猜你喜欢
  • 2021-07-23
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 2011-08-19
  • 2011-09-23
相关资源
最近更新 更多