Error while trying to move file to a folder with PHP and JavaScript

0

I'm trying to move files from a folder. These files are listed, from the moment they are created, I want to move them to the end of the day for a given back-up folder. However, by calling the PHP function responsible for this, nothing does.

The following is the code below:

<html>
  <head>
   <meta charset="UTF-8">
  <meta http-equiv="refresh" content="50000">
        <title></title>
        <head>
                 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scripttype="text/javascript">
            function moveFile(fileName) {
              $.ajax({
                url:'copyFile.php',
                type: "GET",
                complete: function (fileName) {
                 alert("Arquivo foi movido!");
               },
               error: function () {
                alert('Erro');
              }
            });  
              return false;
            }
          </script>
        </head>
      </head>
      <body>
          <?php



      if(isset($_GET['path']))
        $dir =  $_GET['path'];
      else
        $dir = 'fileInfo/';
      foreach (new DirectoryIterator($dir) as $file) {
        if (!$file->isDot()) {
          if ($file->isDir()) {
            echo '<div>';
            echo '<a href="index.php?path='.$dir. DIRECTORY_SEPARATOR .$file.'"></a><br/>' . $file->getBaseName();
            echo '</div>';
          } else {
            switch($file->getExtension()) {
              case 'txt':
              echo "<a href='".$dir.$file."'>".$file."</a>"  ?> <button onclick="moveFile('<?php echo $file?>')">Mover arquivo</button>
              <?php
              break;
            }
          }

        }
      }

      ?>

PHP that contains the function to transfer the files:

<?php

function moverArquivos($file){

var_dump($file);
$dir = 'files/'.$file;
$destino = 'files/backup/';
copy($dir, $destino);
unlink('files/'.$file);

}
?>
    
asked by anonymous 23.10.2016 / 01:39

2 answers

2

From what I know of the copy function of php, its function has the error of only having defined the destination folder when it is necessary to define the whole file name try it like this:

function moverArquivos($file){
    $fonte = 'files/'.$file;
    $copia = 'files/backup/'.$file;
    $res = copy($fonte , $copia);
    if($res) echo "Arquivo copiado!";
    $res = unlink($fonte);
    if($res) echo "Arquivo original apagado";
}

that its function would look better this way:

function moverArquivos($file){
    $fonte = 'files/'.$file;
    $copia = 'files/backup/'.$file;
    $res = rename($fonte , $copia); //A função rename retorna true se teve sucesso ou false se houve falha.
    if($res):
        echo "Arquivo copiado!";
    else:
        echo "Falha ao copiar o arquivo: ".$file;
    endif;
}
    
23.10.2016 / 08:18
1

Ajax alone can not call its function, it is also necessary to pass the file to its function by declaring the date attribute on the scorer of its $ .ajax function.

    <html>
        <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="50000">
        <title></title>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scripttype="text/javascript">
                function moveFile(fileName) {
                  $.ajax({
                    url:'copyFile.php',
                    type: "GET",
                    data:{
                      "file":fileName
                    },
                    complete: function (fileName) {
                     alert("Arquivo foi movido!");
                   },
                   error: function () {
                    alert('Erro');
                  }
                });  
                  return false;
                }
        </script>
    </head>
    <body>
    <?php
        if(isset($_GET['path']))
            $dir =  $_GET['path'];
        else
            $dir = 'fileInfo/';
        foreach (new DirectoryIterator($dir) as $file) {
            if (!$file->isDot()) {
                if ($file->isDir()) {
                    echo '<div>';
                    echo '<a href="index.php?path='.$dir. DIRECTORY_SEPARATOR .$file.'"></a><br/>' . $file->getBaseName();
                    echo '</div>';
                } else {
                    switch($file->getExtension()) {
                      case 'txt':
                      echo "<a href='".$dir.$file."'>".$file."</a>"  ?> <button onclick="moveFile('<?php echo $file?>')">Mover arquivo [<?php echo $file?>]</button>
                      <?php
                      break;
                    }
                }
            }
        }
    ?>

And in your copyFile.php file you get $ _GET with the value passed by the function in js and calls the function in php.

    <?php

    if(isset($_GET['file'])){
        moverArquivos($_GET['file']);
    }

    function moverArquivos($file){

    var_dump($file);
    $dir = 'files/'.$file;
    $destino = 'files/backup/';
    copy($dir, $destino);
    unlink('files/'.$file);
    echo "Arquivo Movido!";
    }
    ?>
    
23.10.2016 / 06:40