plugin development - Wordpress Playground and uploading permission issue

admin2025-01-08  3

I've been testing the Wordpress Playground with my plugin. My plugin offers a way to upload a file to wp-content/uploads/ and it seems to work in the playground.

$file_name = $file['name'];
$temp_file = $file['tmp_name'];

//Move uploaded file to Wordpress upload folder
$upload_dir = wp_upload_dir();
$destination = $upload_dir['basedir'] . '/' . $file_name;
    

if (move_uploaded_file($temp_file, $destination)) 
{
    $file_url = $upload_dir['baseurl'] . '/' . $file_name;    
    echo json_encode( __('File uploaded successfully', 'csv-to-html') . ': ' . $file_url);
} 

The file uploaded successfully prints out and I see that file is in that location (I have a function that scans a folder (wp-content/uploads) and I see that the file exists there), but WHEN I try to read this file into an array I get a permission denied.

So I can write the file but I can't read it. The code is for reading is:

 //$file is path to wp-content/uploads/ with the actual filename 
 //included, e.g. wp-content/uploads/books.csv
 $arr_from_file = array_filter( file( $file, FILE_IGNORE_NEW_LINES ) ); 

This code works perfect locally and on other external sites. The plugin has 500+ active users, so I really don't know how to do a workaround to get this to work in the Wordpress playground. Thoughts?

I've been testing the Wordpress Playground with my plugin. My plugin offers a way to upload a file to wp-content/uploads/ and it seems to work in the playground.

$file_name = $file['name'];
$temp_file = $file['tmp_name'];

//Move uploaded file to Wordpress upload folder
$upload_dir = wp_upload_dir();
$destination = $upload_dir['basedir'] . '/' . $file_name;
    

if (move_uploaded_file($temp_file, $destination)) 
{
    $file_url = $upload_dir['baseurl'] . '/' . $file_name;    
    echo json_encode( __('File uploaded successfully', 'csv-to-html') . ': ' . $file_url);
} 

The file uploaded successfully prints out and I see that file is in that location (I have a function that scans a folder (wp-content/uploads) and I see that the file exists there), but WHEN I try to read this file into an array I get a permission denied.

So I can write the file but I can't read it. The code is for reading is:

 //$file is path to wp-content/uploads/ with the actual filename 
 //included, e.g. wp-content/uploads/books.csv
 $arr_from_file = array_filter( file( $file, FILE_IGNORE_NEW_LINES ) ); 

This code works perfect locally and on other external sites. The plugin has 500+ active users, so I really don't know how to do a workaround to get this to work in the Wordpress playground. Thoughts?

Share Improve this question edited May 9, 2024 at 13:08 bestprogrammerintheworld asked May 9, 2024 at 12:53 bestprogrammerintheworldbestprogrammerintheworld 1,3212 gold badges19 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I ran into this problem also with a plugin I am setting up a Playground Blueprint for.

What I found is that when the user attempts to upload an image via my plugin, the permissions on the file end up being 0000. Which of course can't be accessed. What I ended up doing is setting the permissions on the uploaded file to 0644 after it is uploaded.

I'm using a custom plugin that handles compatibility issues with Playground, so I added a handler function to it that sets the file permissions when an upload is completed.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266469a1123.html

最新回复(0)