Php 读取存储在媒体文件夹中的excel文件
我正在开发一个WordPress插件,需要上传一个excel文件,然后读取其中的数据 我正在使用“media\u handle\u upload”功能上载excel文件,然后,当我尝试使用SpreadsheetReader读取该文件时,出现一个错误,表示该文件不可读 这就是我正在编写的代码,(我知道这是一个可怕的代码,但我正在学习,这只是为了让插件正常工作)Php 读取存储在媒体文件夹中的excel文件,php,wordpress,wordpress-plugin-creation,Php,Wordpress,Wordpress Plugin Creation,我正在开发一个WordPress插件,需要上传一个excel文件,然后读取其中的数据 我正在使用“media\u handle\u upload”功能上载excel文件,然后,当我尝试使用SpreadsheetReader读取该文件时,出现一个错误,表示该文件不可读 这就是我正在编写的代码,(我知道这是一个可怕的代码,但我正在学习,这只是为了让插件正常工作) if(isset($\u文件['test\u upload\u pdf'])){ $pdf=$_文件['test_upload_pdf']
if(isset($\u文件['test\u upload\u pdf'])){
$pdf=$_文件['test_upload_pdf'];
//使用wordpress功能上传
//test\u upload\u pdf对应于$\u FILES数组中的位置
//0表示内容未与任何其他帖子关联
$uploaded=media\u handle\u upload('test\u upload\u pdf',0);
//使用WP函数进行错误检查
如果(是错误($上传)){
echo“上传文件时出错:”.$upload->get_Error_message();
}否则{
echo$pdf.“
”;
$year=日期('Y');
$month=日期('m');
$targetPath=get_site_url()。“/wp content/uploads/”$year./“$month./”$_文件['test_upload_pdf']['name'];
$targetPath2=$\u服务器['DOCUMENT\u ROOT'].“/”$\u服务器[“HTTP\u主机”]。“/wp content/uploads/”。“$year./”。“/”$\u文件['test\u upload\u pdf'.['name'];
回声“
”;
echo$targetPath2;
回声“
”;
尝试
{
$Reader=新的电子表格阅读器($targetPath2);
}
捕获(例外$E)
{
echo$E->getMessage();
}
我认为读卡器无法工作,因为它试图访问“localhost”而不是物理文件夹,例如,$targetPath中的一个打印了以下内容:
所以…我的问题是,有一种方法可以访问媒体文件,这样我就可以用阅读器打开它们了
谢谢。也许这有助于:
否则,请尝试硬编码文件夹“/your/path/to/upload/folder/file.extension”,并了解如何创建此程序
if(isset($_FILES['test_upload_pdf'])){
$pdf = $_FILES['test_upload_pdf'];
// Use the wordpress function to upload
// test_upload_pdf corresponds to the position in the $_FILES array
// 0 means the content is not associated with any other posts
$uploaded=media_handle_upload('test_upload_pdf', 0);
// Error checking using WP functions
if(is_wp_error($uploaded)){
echo "Error uploading file: " . $uploaded->get_error_message();
}else{
echo $pdf."<br>";
$year = date('Y');
$month = date('m');
$targetPath = get_site_url()."/wp-content/uploads/".$year."/".$month."/".$_FILES['test_upload_pdf']['name'];
$targetPath2 = $_SERVER['DOCUMENT_ROOT']."/".$_SERVER["HTTP_HOST"]."/wp-content/uploads/".$year."/".$month."/".$_FILES['test_upload_pdf']['name'];
echo "<br>";
echo $targetPath2;
echo "<br>";
try
{
$Reader = new SpreadsheetReader($targetPath2);
}
catch (Exception $E)
{
echo $E -> getMessage();
}