You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP网站能否读取个人OneDrive中的CSV文件?

Solution: Read CSV from OneDrive in Your Local XAMPP Site

Hey there! Let's figure out how to make your local website pull the updated CSV from your OneDrive, since you can't use FTP from your work computer. Here's a straightforward approach that should work for you:

Step 1: Make Your OneDrive CSV Publicly Accessible

First, you need to set up your CSV file on OneDrive so your PHP code can fetch it:

  • Open OneDrive in your browser, locate your test.csv file.
  • Right-click the file and select Share.
  • In the sharing settings, set the permission to Anyone with the link can view (only people with the exact link will be able to access it).
  • Get the direct download link (not the preview link). If your shared link looks like https://onedrive.live.com/embed?cid=XXX&resid=XXX&authkey=XXX, modify it to a download link by replacing embed with download, so it becomes https://onedrive.live.com/download?cid=XXX&resid=XXX&authkey=XXX. Alternatively, look for a "Copy direct link" option in the sharing menu if it's available.

Step 2: Modify Your PHP Code to Fetch the OneDrive CSV

Your original code reads a local file—we'll adjust it to pull the CSV from your OneDrive link instead. Here's the updated code:

<?php
// Replace this with your OneDrive direct download link
$onedrive_csv_url = "https://your-onedrive-direct-download-link/test.csv";

// Fetch the CSV content from OneDrive
$csv_content = file_get_contents($onedrive_csv_url);
if (!$csv_content) {
    die("Oops! Couldn't fetch the CSV from OneDrive. Check if your link is correct and the file is public.");
}

// Create a temporary in-memory stream to work with fgetcsv (since fgetcsv works better with file handles)
$temp_stream = fopen('php://memory', 'r+');
fwrite($temp_stream, $csv_content);
rewind($temp_stream); // Reset the stream pointer to the start

// Get header row (same as your original code)
$row = fgetcsv($temp_stream);
?>
<table border="1">
<thead align="center">
<tr>
<td><?= $row[0] ?></td>
<!-- <td><?= $row[1] ?></td> This column is not needed hence being commented out -->
<td><?= $row[2] ?></td>
<td><?= $row[3] ?></td>
<td><?= $row[4] ?></td>
<td><?= $row[5] ?></td>
<td><?= $row[6] ?></td>
<td><?= $row[7] ?></td>
<td><?= $row[8] ?></td>
<td><?= $row[9] ?></td>
<td><?= $row[10] ?></td>
<td><?= $row[11] ?></td>
<td><?= $row[12] ?></td>
</tr>
</thead>
<tbody>
<?php
// Loop through the rest of the rows (same as your original code)
while ( ($row = fgetcsv($temp_stream)) !== false ) {
?>
<tr>
<td><?= $row[0] ?></td>
<!-- <td><?= $row[1] ?></td> Not needed as this is the class number -->
<td><?= $row[2] ?></td>
<td><?= $row[3] ?></td>
<td><?= $row[4] ?></td>
<td><?= $row[5] ?></td>
<td><?= $row[6] ?></td>
<td><?= $row[7] ?></td>
<td><?= $row[8] ?></td>
<td><?= $row[9] ?></td>
<td><?= $row[10] ?></td>
<td><?= $row[11] ?></td>
<td><?= $row[12] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
// Clean up the temporary stream
fclose($temp_stream);
?>

Important Notes to Avoid Issues

  • Check allow_url_fopen: XAMPP usually has this enabled by default, but if you get errors, open your php.ini file (in XAMPP's php folder), find allow_url_fopen = On, make sure it's not commented out, then restart Apache.
  • Bypass Caching: Sometimes browsers or servers cache the old CSV. To fix this, add a timestamp to your link to force a fresh fetch every time:
    $onedrive_csv_url = "https://your-onedrive-link/test.csv?" . time();
    
  • Fix Garbled Text: If your CSV shows weird characters, it's an encoding issue. Use mb_convert_encoding to adjust it to UTF-8 (most web-friendly encoding):
    $csv_content = mb_convert_encoding($csv_content, 'UTF-8', 'GBK'); // Replace 'GBK' with your CSV's actual encoding if needed
    

内容的提问来源于stack exchange,提问作者Lensmeister

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:19:13