Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#49658 new defect (bug)

Incorrect Upload Directory path using wp_upload_dir()

Reported by: amitkumarsingh's profile amitkumarsingh Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.2
Component: Upload Keywords: reporter-feedback
Focuses: Cc:

Description (last modified by SergeyBiryukov)

I usually don't use windows when coding with WordPress.
I was creating a file in the uploads directory and decided to use wp_upload_dir() to get the upload directory path. When I printed the result, I noticed that the path returned was incorrect.
Below is the print_r result of the wp_upload_dir() function:

Array
(
    [path] => C:\xampp\htdocs\test/wp-content/uploads/2020/03
    [url] => http://localhost/jan/wp-content/uploads/2020/03
    [subdir] => /2020/03
    [basedir] => C:\xampp\htdocs\test/wp-content/uploads
    [baseurl] => http://localhost/jan/wp-content/uploads
    [error] => 
)

As soon as the path enters in the WordPress directory, backslashes are converted to front slashes.

Attachments (1)

vendor.zip (620.8 KB) - added by amitkumarsingh 5 years ago.

Download all attachments as: .zip

Change History (5)

#1 @siliconforks
5 years ago

I don't think this is actually a problem - both forward slashes and backslashes can be used as directory separators on Windows. See also #44654.

#2 @amitkumarsingh
5 years ago

Thanks for the reply.
While I see what you say about the forward and backward slashes to be true when accessing through file explorer, I still get a 500 Internal Server error when using the PATH retrieved by wp_upload_dir() function.
Whereas there are no issues when I use wp_upload_dir() on Linux.
Trac #44654 does make a good point that DIRECTORY_SEPARATOR should be used.
But, the point here to note is the path is being returned by the function and not by plugin code.

#3 @SergeyBiryukov
5 years ago

  • Component changed from General to Upload
  • Description modified (diff)
  • Keywords reporter-feedback added; needs-patch removed

Hi there, welcome back to WordPress Trac! Thanks for the ticket.

Using DIRECTORY_SEPARATOR in core has been discussed several times before, most recently in #28811, #29726, and #44654. Since Windows recognizes both types of slashes, there is no strong reason for changing this if everything works as expected.

Anywhere in core or plugins where paths need to be compared, wp_normalize_path() should be used.

Some more previous discussions: #15598, #16457, #17494, #20849.

I still get a 500 Internal Server error when using the PATH retrieved by wp_upload_dir() function.

Could you provide the steps to reproduce the issue on a clean install? How are you using the path?

@amitkumarsingh
5 years ago

#4 @amitkumarsingh
5 years ago

Hi sorry for the delay on this.
Below is the code I am using:

add_action( 'init', 'file_creation_test' );
function file_creation_test() {
	require_once plugin_dir_path(__DIR__) . '/vendor/autoload.php';
	$upload_dir     = wp_upload_dir();
	$wqr_upload_dir = $upload_dir['basedir'] . '/testing';

	$php_word = new \PhpOffice\PhpWord\PhpWord();
	$section = $php_word->addSection();
	$section->addText( 'Word Test file' );

	$obj_writer = \PhpOffice\PhpWord\IOFactory::createWriter( $php_word, 'Word2007' );
	$obj_writer->save( $wqr_upload_dir . '/test_word.docx' );
}

I am using PhpOffice/PhpWord to create a document. I have attached its library above.

Note: See TracTickets for help on using tickets.