I need some assistance here. So far, i build a plugin, that are able to import data of a CSV file and parse it into fields of a custom post type.

At this point, I would like to make a form to display in admin panel, from were the admin can upload a new CSV file from.

The custom form i have build is displayed below.

// 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

        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>';

The following function is handling the file upload. Within the function i included some error handling. See below:

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); 


    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>';



The error handling when executing is outputting following lines:

  • File upload button was clicked!

  • File upload function is now running!

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-content\plugins\fiske-makker-data-importer/uploads/

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-content\plugins\fiske-makker-data-importer\data-importer.php:164:string 'C:\xampplite\tmp\phpDA2F.tmp' (length=28)

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-content\plugins\fiske-makker-data-importer\data-importer.php:165:string 'C:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-adminC:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-content\plugins\fiske-makker-data-importer/uploads/fredningszoner-kort.csv' (length=191)

  • An error occurred somewhere. Try again or contact the admin

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev.cc\wp-content\plugins\fiske-makker-data-importer\data-importer.php:194:boolean false

So the function keeps resulting in the boolean "$didUpload" being false, which indicates, that the files was not upload.

I need some suggestions to areas i could troubleshoot within this function or some code review, if someone see an error, that i missed myself.

Thanks in advance!

1 Answer 1


I don't know what the reason you upload a file to the plugin directory. The standard way to upload a file is to the WordPress uploads directory.

In your code, I find this one is wrong

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

See, you merge two path together. Try

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

But again, it is not the correct way, you should upload files to wp_upload_dir() instead.

  • Duh ! You are completely right. The path was merged. Problem solved!
    – Dougless
    Commented Feb 3, 2019 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.