简体   繁体   中英

wordpress copy plugin uploaded file to theme dir

I have a line of code that is supposed to copy image from one directory to another:

copy("http://localhost:8080/wordpress/wp-content/uploads/2015/06/uploaded_background.jpg","http://localhost:8080/wordpress/wp-content/themes/landing-page/img/desktop-background.jpg");

This is located in wordpress theme header.php file, however I am receiving an error message:

Warning: copy( http://localhost:8080/wordpress/wp-content/themes/landing-page/img/desktop-background.jpg ): failed to open stream: HTTP wrapper does not support writeable connections in C:\\wamp\\www\\wordpress\\wp-content\\themes\\landing-page\\header.php on line 42

Line 42 is where my copy function line is located.

How can I solve this issue. Any suggestions or links would help a lot.

You should use a local path as a 2nd parameter to copy(), since http protocol does not support file overwriting.

The destination path. If dest is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files. Source : http://php.net/manual/en/function.copy.php

instead of using

 copy("http://localhost:8080/wordpress/wp-content/uploads/2015/06/uploaded_background.jpg","http://localhost:8080/wordpress/wp-content/themes/landing-page/img/desktop-background.jpg");

try

copy`("/yourserverfolderpath/wordpress/wp-content/uploads/2015/06/uploaded_background.jpg","/yourserverfolderpath//wordpress/wp-content/themes/landing-page/img/desktop-background.jpg");`

yourserverfolderpath/ could be something like /home/user/abc/

see you ftp or cpanel for exact path

see this SO question as guideline

PS: In case of local file, path would be something like D:\\php\\www\\www\\xml.php

as pointed by @dlegall in comments and as per documentation, First param of copy() can be a valid URL, see the docs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM