Read an excel file stored in media folder

General Tech Bugs & Fixes 3 years ago

6.55K 1 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

I'm developing a WordPress plugin that requires to upload an excel file and then read the data inside of it.

I'm using the function "media_handle_upload" to upload the excel file, then, when I try to use SpreadsheetReader to read the file there's an error saying that the file is not readable.

This is what I'm coding, (I know it's horrible coding, but I'm learning and this is just to get the plugin working)

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."
"
; $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 "
"
; echo $targetPath2; echo "
"
; try { $Reader = new SpreadsheetReader($targetPath2); } catch (Exception $E) { echo $E -> getMessage(); }

I think the reader doesn't work because is trying to access "localhost" instead of the physical folder, for example, one of the $targetPath prints this:

http://127.0.0.1/wordpress/wp-content/uploads/2019/04/example.xlsx

So... My question is, there's a way to access the media files so I can open them with the reader?

Thanks.

0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum