无法在管理面板中使用自定义上传表单将CSV文件上传到插件目录

时间:2019-02-03 作者:Dougless

我需要一些帮助。到目前为止,我构建了一个插件,可以导入CSV文件的数据,并将其解析为自定义帖子类型的字段。

在这一点上,我想做一个表格显示在管理面板,从是管理员可以上传一个新的CSV文件。

下面显示了我生成的自定义表单。

// Check whether the button has been pressed AND also check the nonce
        if (isset($_POST[\'submit\'])){

            echo \'<p>File upload button was clicked!</p>\';
            // the button has been pressed AND we\'ve passed the security check
            file_upload_action();
        }

        echo \'<form action="?page=csv-data-importer-slug" method="post" enctype="multipart/form-data">\';

            echo \'<p>Upload a File:</p>\';

            echo \'<input type="file" name="myfile" id="fileToUpload">\';
            echo \'<input type="hidden" name="submit">\';
            submit_button(\'Upload File Now\');
        echo \' </form>\';
以下函数正在处理文件上载。在函数中,我包含了一些错误处理。见下文:

function file_upload_action() {

    $enableimport = true; 

    echo "<p>File upload function is now running!</p>";

    $currentDir = getcwd();
    $uploadDirectory = plugin_dir_path( __FILE__ ) . "uploads/";

    echo $uploadDirectory;

    $errors = []; // Store all foreseen and unforseen errors here

    $fileExtensions = [\'csv\']; // Get all the file extensions

    $fileName = $_FILES[\'myfile\'][\'name\'];
    $fileSize = $_FILES[\'myfile\'][\'size\'];
    $fileTmpName  = $_FILES[\'myfile\'][\'tmp_name\'];
    $fileType = $_FILES[\'myfile\'][\'type\'];
    $fileExtension = strtolower(end(explode(\'.\',$fileName)));

    $uploadPath = $currentDir . $uploadDirectory . basename($fileName); 

    var_dump($fileTmpName);
    var_dump($uploadPath);

    if (isset($_POST[\'submit\'])) {

        if (! in_array($fileExtension,$fileExtensions)) {
            $errors[] = \'<p>This file extension is not allowed. Please upload a CSV file</p>\';
        }

        if ($fileSize > 2000000) {
            $errors[] = \'<p>This file is more than 2MB. Sorry, it has to be less than or equal to 2MB</p>\';
        }

        if (empty($errors)) {
            $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

            if ($didUpload) {
                echo \'<p>The file \' . basename($fileName) . \' has been uploaded</p>\';


            } else {
                echo \'<p>An error occurred somewhere. Try again or contact the admin</p>\';
            }
        } else {
            foreach ($errors as $error) {
                echo $error . \'<p>These are the errors\' . \'\\n\' . \'</p>\';
            }
        }
    }

    var_dump($didUpload);

    return;
}
执行时的错误处理是输出以下行:

已单击文件上载按钮!

文件上传功能正在运行!

C:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp content\\plugins\\fiske makker数据导入器/上载/

C:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp content\\plugins\\fiske makker数据导入器\\数据导入器。php:164:string\'C:\\xampplite\\tmp\\phpDA2F。tmp’(长度=28)

C:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp content\\plugins\\fiske makker数据导入器\\数据导入器。php:165:string\'C:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp adminC:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp content\\plugins\\fiske makker数据导入器/上传/fredningszoner kort。csv’(长度=191)

某处出错。重试或联系管理员

C:\\Users\\kim\\u a\\Desktopserver\\www.fiske-makker。dev.cc\\wp content\\plugins\\fiske makker数据导入器\\数据导入器。php:194:布尔值假

因此该函数一直导致布尔值“$didUpload”为false,这表示文件未上载。

如果有人看到我错过了自己的错误,我需要一些建议来解决这个函数中可能出现的问题,或者进行一些代码检查。

提前感谢!

1 个回复
最合适的回答,由SO网友:hamdirizal 整理而成

我不知道你为什么把文件上传到插件目录。上传文件的标准方式是到WordPressuploads 目录

在你的代码中,我发现这个是错误的

$uploadPath = $currentDir . $uploadDirectory . basename($fileName); 
看,您将两条路径合并在一起。尝试

$uploadPath = $uploadDirectory . basename($fileName); 
但同样,这不是正确的方法,您应该将文件上载到wp_upload_dir() 相反