Error trying to move file to a folder with PHP and Javascript

Asked

Viewed 505 times

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 at the end of the day to a certain back-up folder. However, when calling Function PHP responsible for this, nothing does.

Follow 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>
          <script type="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 files:

<?php

function moverArquivos($file){

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

}
?>

2 answers

2


From what I know about php’s copy function, its function has the error of only having set the destination folder when it is necessary to define the whole name of the file 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";
}

Although from what I saw in the function you are wanting to move the file not to copy since you are deleting the original, php does not have a move() function but has the Rename() function that does the same thing, so I think its function would look better like this:

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;
}

1

ajax alone cannot call its function, it is also necessary to pass the file to its function by declaring the date attribute in the middle 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>
        <script type="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 js function and call the php function.

    <?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!";
    }
    ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.