最新消息: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)

uploads - Plugin writing: access file that was just uploaded

matteradmin9PV0评论

I am having trouble finding a solution for following problem:

The user should upload a file in the menu of my plugin; the file is uploaded to the media folder by using media_handle_upload(file), where the file comes from the _FILES array - this works perfectly. The plugin then, however, should access that very same file (CSV) to extract data and present it on a page via a shortcode.

I can not, however, find out how to access that file in my plugin. I have looked into this: Access to Media Library, this: Accessing Media/Files outside the_content, and I have tried to find something in the wordpress codex. So far I could not find anything on this matter, however.

At the moment, my code for reading the file's contents looks like this:

$my_file = trailingslashit( wp_upload_dir() ) . 'MyFile.csv';
if (file_exists($my_file)) {
    return file_get_contents($my_file, FILE_USE_INCLUDE_PATH);
} else {
    return "No file found";
}

Thus my question is: how does my plugin access a file that is uploaded into the media folder but is not connected to any post or the like? Again, I intend to read the file's contents and process them further.

I am having trouble finding a solution for following problem:

The user should upload a file in the menu of my plugin; the file is uploaded to the media folder by using media_handle_upload(file), where the file comes from the _FILES array - this works perfectly. The plugin then, however, should access that very same file (CSV) to extract data and present it on a page via a shortcode.

I can not, however, find out how to access that file in my plugin. I have looked into this: Access to Media Library, this: Accessing Media/Files outside the_content, and I have tried to find something in the wordpress codex. So far I could not find anything on this matter, however.

At the moment, my code for reading the file's contents looks like this:

$my_file = trailingslashit( wp_upload_dir() ) . 'MyFile.csv';
if (file_exists($my_file)) {
    return file_get_contents($my_file, FILE_USE_INCLUDE_PATH);
} else {
    return "No file found";
}

Thus my question is: how does my plugin access a file that is uploaded into the media folder but is not connected to any post or the like? Again, I intend to read the file's contents and process them further.

Share Improve this question asked Apr 2, 2019 at 18:32 YatoYato 175 bronze badges 2
  • Welcome to WordPress StackExchange. You can get attachment id from media_handle_upload() function. If you can get the id of the uploaded file you can use get_attached_file() function for getting the full path of the file. Referral of function: codex.wordpress/Function_Reference/get_attached_file – Serkan Algur Commented Apr 2, 2019 at 18:43
  • 1 that worked, thank you! I don't know how I missed that – Yato Commented Apr 4, 2019 at 8:21
Add a comment  | 

1 Answer 1

Reset to default 1

When you use media_handle_upload() to upload a file, it creates the attachment post in the database and return the ID of the attachment, or a WP_Error if the upload failed. This ID of the attachment is used to access uploaded file.
See documentation here. and an example here.

So path to your CSV file can be retrieved using get_attached_file() as follows:

$my_file = get_attached_file( $attachment_id ); // Full path

Documentation for get_attached_file() is here.
I hope this may help.

Post a comment

comment list (0)

  1. No comments so far