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.
media_handle_upload()
function. If you can get the id of the uploaded file you can useget_attached_file()
function for getting the full path of the file. Referral of function: codex.wordpress.org/Function_Reference/get_attached_file