binary file content shown in php print_r but not saved in mysql

后端 未结 1 1066
温柔的废话
温柔的废话 2020-12-11 11:38

i have to save the files from input fields to database,i\'ll do it this way:

    $mkey = mysqli_insert_id($mysqli);
    $i=0;
    while (isset($_FILES[\'file         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 12:24

    RTM ;-)

    If data size of a variable exceeds max. allowed packet size (max_allowed_packet), you have to specify b in types and use mysqli_stmt_send_long_data() to send the data in packets.

    So I have never done this myself but i would assume it needs to look something like this based on your code and the example on the functions doc page:

        $filepath = ini_get('upload_tmp_dir')."/".basename($_FILES['file'.$i]['tmp_name']);
        $filepath = addslashes($filepath);
        $handle = fopen($filepath, "rb");
        $content = null;
    
        $stmt = $mysqli->prepare("INSERT INTO attachment (filename,filecontent,mkey) VALUES (?,?,?)");
        $stmt->bind_param("sbi",$_FILES['file'.$i]['name'], $content, $mkey);
    
        while (!feof($handle)) {
            // $maxPacketSize would be the size of your max packet setting for mysql,
            // or something safely assumed to be below it
            $stmt->send_long_data(1, fread($handle, $maxPacketSize));
        }
        fclose($handle);
        $stmt->execute();
    

    0 讨论(0)
提交回复
热议问题