最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Can't upload CSV file to plugin directory using custom upload form in admin panel

matteradmin10PV0评论

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

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

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

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\wp-content\plugins\fiske-makker-data-importer/uploads/

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev\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\wp-content\plugins\fiske-makker-data-importer\data-importer.php:165:string 'C:\Users\kim_a\Desktopserver\www.fiske-makker.dev\wp-adminC:\Users\kim_a\Desktopserver\www.fiske-makker.dev\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\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!

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

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

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

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\wp-content\plugins\fiske-makker-data-importer/uploads/

  • C:\Users\kim_a\Desktopserver\www.fiske-makker.dev\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\wp-content\plugins\fiske-makker-data-importer\data-importer.php:165:string 'C:\Users\kim_a\Desktopserver\www.fiske-makker.dev\wp-adminC:\Users\kim_a\Desktopserver\www.fiske-makker.dev\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\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!

Share Improve this question asked Feb 3, 2019 at 9:04 DouglessDougless 751 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far