• Hello
    i have a problem ,i have a plugin which will move a file after been uploaded
    and put it in a specific folder ,,
    i tested the code on a normal page which i created in the root
    and it is working fine ,but when it is in the plugin code, it is not working
    the code is pretty simple
    $file = “./wp-content/uploads/jacket.jpg”;
    $newfile=”./uploads/jacket.jpg”;
    if(!copy($file, $newfile)) {
    error_log(“failed to copy $file…”);
    }
    ofcourse i tested the plugin code if it is running ,and i am getting error log in wamp that failed to copy ,,help i am hopeless

Viewing 2 replies - 1 through 2 (of 2 total)
  • I suspect that the problem is with the current directory. When the code runs in a page, it has ultimately been run from the index.php file in the top level of the website, hence “.” = the current directory, is this top directory, and your file names like “./wp-content/thing” make sense. (Presumably you do have a top level directory “./uploads”.

    BUT when the code runs in a plugin, what is the current directory ?
    Suggest that you add to the error report

    if(!copy($file, $newfile)) {
    error_log(“failed to copy $file…” . ‘X’ . getcwd() .’X’);
    }

    What does it say now ?

    Dion

    (@diondesigns)

    You should be using the ABSPATH constant, as follows:

    $file = ABSPATH . 'wp-content/uploads/jacket.jpg';
    $newfile = ABSPATH . 'uploads/jacket.jpg';
    if(!copy($file, $newfile)) {
    	error_log("failed to copy $file...");
    }

    If this is for your own use, then the above is fine. If you will be distributing the plugin, then please make sure to read the following:

    http://codex.ww.wp.xz.cn/Determining_Plugin_and_Content_Directories

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘copy function does not work, move files and creat foulder’ is closed to new replies.