readfile 相关问题

ReadFile()是一个Windows API函数,用于从指定文件或输入/输出(I / O)设备读取数据。如果设备支持,则读取发生在文件指针指定的位置。

如何正确使用 async/await 读取文件?

我无法弄清楚异步/等待是如何工作的。我稍微理解了它,但我无法让它发挥作用。 函数 loadMonoCounter() { fs.readFileSync("monolitic.txt", "二进制", 异步

回答 13 投票 0

如何读取文件并将内容保存在字符串中? (C语言,没有崩溃或核心转储错误)

我编写了一个读取文件的代码。读取默认文件或其他文件并将内容文件存储在字符串中。我不知道我哪里错了!当我运行它时,有时没问题,但有时我会得到......

回答 1 投票 0

fs.readFileSync("./bank/"+client,'cp1251');正在抛出新的 ERR_INVALID_OPT_VALUE_ENCODING

我正在尝试从文件中读取(该文件是保加利亚语),并且使用 utf 8 它会返回无意义的字符,因此我尝试了 cp1251 但它抛出:ERR_INVALID_OPT_VALUE_ENCODING。 var str = fs.readFileSync("./b...

回答 2 投票 0

在文件中每行的开头和结尾添加一个字符

我有一个文本文件,每一行只包含一个单词。我想通过将一个字符附加到每行中每个单词的开头和结尾来更新文件。这是我的 Python 代码: 附加文本='_'

回答 4 投票 0

尝试读取空手道中的/资源中的文件并获取:无法找到或读取文件

阅读此内容:https://github.com/karatelabs/karate#reading-files 它说明了如何设置 pom.xml 以限制类路径不读取/资源。我可以知道类路径的 pom.xml 应该是什么吗...

回答 1 投票 0

读取输入文件,但输出不符合我的预期

我的文件包含“abcdefg” 为什么当我使用这部分代码时,在控制台中,它会打印字母“g”。 `#include #包括 #包括 使用命名空间...

回答 1 投票 0

PHP echo() 损坏 readfile() 输出

我有这个PHP脚本(process.php): 我有这个 PHP 脚本(process.php): <?php // Include necessary libraries for database, PDF generation, and ZIP compression. require_once('db_connection.php'); require_once('tcpdf/tcpdf.php'); if (isset($_POST['submit'])) { // Get uploaded file data $csvFile = $_FILES['csv_file']['tmp_name']; // Initialize progress variables $totalRecords = count(file($csvFile)) - 1; // Subtract 1 for the header row $processedRecords = 0; // Create a temporary directory to store the PDF files $tempDir = '/var/www/html/insuromatic/temp/'; if (!file_exists($tempDir)) { mkdir($tempDir, 0777, true); } // Create a ZipArchive instance $zip = new ZipArchive(); $zipFileName = 'pdf_archive.zip'; if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) { // Open and read the CSV file if (($handle = fopen($csvFile, "r")) !== FALSE) { // Skip the first row (header) fgetcsv($handle); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // Assuming your CSV columns are in order (column1, column2, column3) $column1 = $data[0]; $column2 = $data[1]; $column3 = $data[2]; // Insert data into the MySQL database $sql = "INSERT INTO import_csv_data (id, name, email) VALUES ('$column1', '$column2', '$column3')"; mysqli_query($conn, $sql); // Generate a PDF for this record $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->AddPage(); $pdf->SetFont('helvetica', '', 12); $pdf->Cell(0, 10, "ID: $column1", 0, 1); $pdf->Cell(0, 10, "Naam: $column2", 0, 1); $pdf->Cell(0, 10, "Email: $column3", 0, 1); // Customize the PDF content as needed // Save the PDF in the temporary directory $pdfFileName = $tempDir . "record_$column1.pdf"; $pdf->Output($pdfFileName, 'F'); // Add the PDF to the ZIP archive $zip->addFile($pdfFileName, "record_$column1.pdf"); // Delete the record from the database $deleteSql = "DELETE FROM import_csv_data WHERE id = '$column1'"; mysqli_query($conn, $deleteSql); // Update progress $processedRecords++; // Send progress to the client // echo "Processed $processedRecords out of $totalRecords records.<br />\n"; // LINE 65 // Flush the output buffer to send data immediately to the client ob_flush(); flush(); // Close the PDF document $pdf->Close(); } fclose($handle); } // Close the ZIP archive $zip->close(); // Remove temporary PDF files array_map('unlink', glob($tempDir . '*.pdf')); rmdir($tempDir); // Provide the ZIP archive for download header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=\"$zipFileName\""); ob_end_clean(); flush(); readfile($zipFileName); // LINE 89 unlink($zipFileName); // Delete the ZIP file after download } // Close the MySQL connection mysqli_close($conn); } ?> 服务器配置: Apache/2.4.57 (Debian) PHP 8.0.30 FPM/FastCGI 问题: 当第 89 行的 readfile() 语句被注释掉时,第 65 行的 echo() 语句工作正常,我看到屏幕上回显了进度 当第 65 行的 echo() 语句被注释掉时,第 89 行的 readfile() 语句工作正常,我得到了包含创建的 PDF 文件的 ZIP 下载 但是,当两条线都启用时,在进度回显到屏幕后,我在屏幕上看到很多垃圾(我认为是原始 PDF 内容)。 ZIP 不提供下载。 不幸的是,我不知道我做错了什么。我尝试了 ob_end_clean()、ob_end_flush() 等几个地方,但没有运气,垃圾不断被打印。有人能指出我正确的方向吗? //编辑:重做版本: <?php // Include necessary libraries for database, PDF generation, and ZIP compression. require_once('db_connection.php'); require_once('tcpdf/tcpdf.php'); if (isset($_POST['submit'])) { // Get uploaded file data $csvFile = $_FILES['csv_file']['tmp_name']; // Initialize progress variables $totalRecords = count(file($csvFile)) - 1; // Subtract 1 for the header row $processedRecords = 0; // Create a temporary directory to store the PDF files $tempDir = '/var/www/html/insuromatic/temp/'; if (!file_exists($tempDir)) { mkdir($tempDir, 0777, true); } // Open and read the CSV file if (($handle = fopen($csvFile, "r")) !== FALSE) { // Skip the first row (header) fgetcsv($handle); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // Assuming your CSV columns are in order (column1, column2, column3) $column1 = $data[0]; $column2 = $data[1]; $column3 = $data[2]; // Insert data into the MySQL database $sql = "INSERT INTO import_csv_data (id, name, email) VALUES ('$column1', '$column2', '$column3')"; mysqli_query($conn, $sql); // Generate a PDF for this record $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->AddPage(); $pdf->SetFont('helvetica', '', 12); $pdf->Cell(0, 10, "ID: $column1", 0, 1); $pdf->Cell(0, 10, "Naam: $column2", 0, 1); $pdf->Cell(0, 10, "Email: $column3", 0, 1); // Customize the PDF content as needed // Save the PDF in the temporary directory $pdfFileName = $tempDir . "record_$column1.pdf"; $pdf->Output($pdfFileName, 'F'); // Delete the record from the database $deleteSql = "DELETE FROM import_csv_data WHERE id = '$column1'"; mysqli_query($conn, $deleteSql); // Update progress $processedRecords++; // Send progress to the client echo "Processed $processedRecords out of $totalRecords records.<br />\n"; // Flush the output buffer to send data immediately to the client ob_flush(); flush(); // Close the PDF document $pdf->Close(); } fclose($handle); } // Close the MySQL connection mysqli_close($conn); // Output a JavaScript script to perform the redirection echo '<script>window.location.href = "download.php";</script>'; //Prevent any further execution exit; } ?> 下载.php: <?php // Specify the directory containing your PDF files $directory = '/var/www/html/insuromatic/temp'; // Define the name of the ZIP archive file $zipFileName = 'pdf_archive.zip'; // Create a ZipArchive object $zip = new ZipArchive(); // Open the ZIP archive for writing if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) { // Create a recursive directory iterator to scan the directory $iterator = new RecursiveDirectoryIterator($directory); $files = new RecursiveIteratorIterator($iterator); // Loop through all files in the directory foreach ($files as $file) { // Check if the file is a PDF if ($file->isFile() && strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'pdf') { // Add the PDF file to the ZIP archive with its original name $zip->addFile($file, $file->getBasename()); } } // Close the ZIP archive $zip->close(); // Remove temporary PDF files $tempDir = '/var/www/html/insuromatic/temp/'; array_map('unlink', glob($tempDir . '*.pdf')); rmdir($tempDir); // Set the appropriate headers for a ZIP file download header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="' . $zipFileName . '"'); header('Content-Length: ' . filesize($zipFileName)); // Send the ZIP file to the client's browser readfile($zipFileName); // Delete the ZIP file from the server (optional) unlink($zipFileName); exit; // Terminate the script } else { echo "Failed to create ZIP archive."; } ?> 为了 ob_end_clean(); 要工作,您需要首先使用启动输出缓冲 ob_start(); 并且不得使用 ob_flush(); 介于两者之间。

回答 1 投票 0

在 php 中显示文件而不阻止应用程序

我正在 Apache2 上提供一个 php 应用程序。在一个函数中,我只想使用 php 脚本显示媒体,而不是将媒体托管在服务器上的公共文件夹中。 这是因为我希望能够...

回答 1 投票 0

如何将ReadFile与wchar_t一起使用?

考虑以下两个函数,第一个函数使用 Windows API 函数 ReadFile() 和 CreateFileW(),而第二个函数使用 fopen() 和 fgetws(),读取非英文文本…

回答 2 投票 0

TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是方法,试图获取存档

我目前正在尝试构建和应用程序来管理预算或费用, 我对Python和一切都很陌生, 所以我从朋友那里得到了帮助,但我不太熟悉...

回答 1 投票 0

为什么在nodejs中读取10个50MB的文件与读取1个500MB的文件花费相同的时间?

//履行一个承诺5000ms console.time('测试'); (异步函数(){ 等待新的 Promise((resolve) => setTimeout(resolve, 5000)); console.log('睡觉了') })() .then(() => { 控制台。

回答 1 投票 0

使用Python读写文件

编写一个 Python 程序,读取名为“input.txt”的文本文件的内容,删除重复行,并将唯一行写入名为“output.txt”的新文件。考虑一下

回答 1 投票 0

Python - 将数据附加到现有文件或读取文件

请编写一个充当简单日记的程序。日记条目应保存在文件 diary.txt 中。当程序执行时,它应该首先读取文件中已有的任何条目。

回答 2 投票 0

读取wav文件时出现运行时错误

我正在关注 https://github.com/mravanelli/SincNet 库。我已按照建议遵循文档。但我得到了运行时错误 我已使用 sudo chmod +x /home/

回答 1 投票 0

DOMException:无法读取请求的文件,通常是由于获取文件引用后发生的权限问题

当我尝试使用 FileReader 读取文件且文件大小为 5.9gb 且当此代码运行时 var file = document.getElementById('uploadFileId').files[0]; 让读者=新的FileReader(); 读者。

回答 3 投票 0

如何写入文件然后删除它

我是一名 C# 程序员,现在需要学习 python。到目前为止,这是我的第二天。 我需要写入一个文件,读取它,然后删除它。 在 C# 中这很容易。 string strPath = @"C: emp est.txt&qu...

回答 3 投票 0

如何使用 showOpenFilePicker 读取文件(不是 <input type="file">)

我想要一个比现在更干净的用户界面 给我。 我可以使用 window.showOpenFilePicker() 获取用户手势安全上下文并获取“fileSystemHandle”

回答 3 投票 0

如何在Java中一次读取每一行,保存该行中的信息,然后移动到下一行

我遇到了这个问题,我认为这基本上只是算法,但尽管我尝试了多种方法,但我还是无法弄清楚。我有一个文件名“employees.txt”。该文件中的每一行都是一条记录...

回答 1 投票 0

如何解决c++中读取文件时的问题

我正在尝试从 .txt 文件中读取信息,但发生了一些非常奇怪的事情。 这是代码: ifstream 文件(目录); 打开(目录); if (fichier.is_open()) 计算<< &

回答 1 投票 0

在JavaSrcipt中读取文件时如何处理错误

我想当读取文件不正确时,记录错误 例如,在下面的代码中,file.txt 没有退出并且无法读取它。我想在控制台中看到 error5 你能帮助我吗? const fs = require('f...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.