简体   繁体   中英

PHP will copy recursive a directory if called via Browser, but not if called via ajax

I have a php script that need to recursive copy a directory to another. It works if called via browser with get variables, but not if called via ajax.

I'm using XAMPP on Windows 7 for testing purposes.

Is there anything about permissions that i may be missing?

    var path = 'http://' + window.location.host + '/wordpress2/wp-content/plugins/pluginname/modules/altera_tema.php';
    jQuery.ajax({
        url: path,
        type: 'GET',
        data: {
            tema: code
        }, 
        complete: function(msg){
            alert(msg.toString());
        }
    });

the code var is the folder name.

  function delete_files($target) {
      if(is_dir($target)){
        $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
          foreach( $files as $file ) {
            delete_files( $file );      
          }
          rmdir( $target );
      } elseif(is_file($target)) {
        unlink( $target );  
      }
  }

  function copyr($source, $dest) {
    if (is_file($source)) {
      return copy($source, $dest);
    }
    if (!is_dir($dest)) {
      mkdir($dest);
    }
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }

      if ($dest !== "$source/$entry") {
         copyr("$source/$entry", "$dest/$entry");
      }
    }
    $dir->close();
    return true;
  }


  delete_files('asdf');
  copyr($_GET['tema'], 'asdf');

this is the PHP that handles the copy and remove of the files

Actually, if i do an ajax request directly in the chrome console, it works perfectly. But it wont work via button / onClick()

I would enable logging for XHR requests in Chrome:

在此处输入图片说明

And then inspect the request from the Network tab:

在此处输入图片说明

Make sure that your Request URL matches the url you're manually entering into your browser. Additional data in the Headers panel will help differentiate the two experiences.

If you also enabled Preserve log upon navigation , you can manually enter the url again and examine the two requests in your Network tab for any significant differences.

Make sure your request type is "GET" in ajax request. when your receiving as get variables

EDIT: as Jonathan Sampson pointed out this assumption is wrong.

i would recommend to add the data parameter directly into the url. Maybe jquery ignores the data: option, assuming it contains post data.

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