我正在创建一个自定义插件,该插件涉及上传 CSV 和使用工作表中的数据的功能。
这里是我用来上传csv文件的函数
<h1 align="center">Upload CSV</h1>
<!-- Upload CSV File-->
<form action="https://wordpress-317991-1667562.cloudwaysapps.com/wp-content/plugins/kdlcsv/file-upload.php" method="post" enctype="multipart/form-data">
Choose the File to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
然后在 file-upload.php
文件中输入以下代码。
//1.0 Load the Basic Files
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require( $parse_uri[0] . '/wp-load.php' );
//2.0 Setting the directory for uploading
$target_dir = "uploads/";
//3.0 Name of the file to upload
$target_file= $target_dir.basename($_FILES["fileToUpload"]["name"]);
//4.0 Variable to check if the upload is properly carried out
$uploadOk=1;
//5.0 Extracting the File Extension
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//6.0 To check if it is an actual file or fake
if(isset($_POST["submit"])){
$check= filesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false){
echo "Actual File is uploaded";
$uploadOk=1;
} else {
echo "File has an error";
$uploadOk=0;
}
}
//7.0 To check if the file already exists in the director
if(file_exists($target_file)){
echo "Sorry, file already exists";
$uploadOk=0;
}
//8.0 To Limit File Size
if($_FILES["fileToUpload"]["size"]>100000000) {
echo "Sorry, your File size is too large";
$uploadOk = 0;
}
//9.0 To Limit File Type
if($imageFileType!="csv" && $imageFileType!="xlsx" && $imageFileType!="xls"){
echo "Sorry only CSV, XLSX and XLS files are allowed to upload";
}
//10.0 complete the file upload
if($uploadOk==0){
echo "Sorry, your file was not uploaded";
}
else {
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file)){
echo "The file ".basename($_FILES["fileToUpload"]["name"])."has been uploaded";
echo basename($_FILES["fileToUpload"]["name"]);
return kdl_csv_to_data_array(basename($_FILES["fileToUpload"]["name"]));
}
else {
echo "Sorry, there was an error uploading your File.";
}
}
//11.0 Function to convert CSV file to Data
function kdl_csv_to_data_array($filename){
//11.1 Test the file name received
echo $filename;
//11.2 The Nested array to hold all the arrays
$the_big_array = [];
//11.3 Open the file for reading
//$upload_dir = wp_upload_dir();
//$h = fopen ("https://wordpress-317991-1667562.cloudwaysapps.com/wp-content/plugins/kdlcsv/uploads/".$filename,"r");
//11.4 Echo the url of the file upload
//$upload_dir = wp_upload_dir();
//echo $upload_dir['basedir'].$filename;
echo "https://wordpress-317991-1667562.cloudwaysapps.com/wp-content/plugins/kdlcsv/uploads/".$filename;
//11.5 Each line in the file is converted into an individual array
while(($data = fgetcsv($h,10000000,",")) !== FALSE){
$the_big_array[] = $data;
}
// //To display the code in a readable format
// echo "<pre>";
// var_dump($the_big_array);
// echo "</pre>";
//return kdl_dataarray_to_tracks($the_big_array);
}
我在 file-upload.php 中只有部分功能是完整的,但我已经在 WordPress 上看到了这个错误。
This page isn’t working wordpress-317991-1667562.cloudwaysapps.com is currently unable to handle this request.
HTTP ERROR 500
您能帮我了解导致实际错误的原因吗?我该如何解决这个问题?