How to use copy ()?

2

I'm creating a project to import files, but I need the file name to use " LOCAL DATA INFILE" , before I was looking for a way to read the temporary file automatically, until I I saw this form of copy .

I want to copy it into the project (tmp folder) to be able to have a name and fixed location for the imported files.

You're returning this error for me: copy() expects parameter 1 to be a valid path, array given in

Here is my code:

$arquivo = $_FILES['arquivo'];
$destino = 'tmp/tmp.txt';

$arquivo_tmp = $_FILES['arquivo']['tmp_name'];
   copy($arquivo, $destino);
    
asked by anonymous 17.08.2018 / 16:28

2 answers

1

This line here:

$arquivo = $_FILES['arquivo'];

$_FILES['arquivo'] is always array containing the upload information. The copy function expects an input with the source and the destination, and the two must be a string .

If you want to get the original client file name, do so:

 $arquivo = $_FILES['arquivo']['name']; // pega o nome do arquivo da máquina do usuário

However, when looking at the context of your code, if you want to copy the upload to another place, then you should use the temporary upload file, which is the actual file generated on the server when uploading a file by the form, like this: / p>

 $arquivo_tmp = $_FILES['arquivo']['tmp_name'];
 copy($arquivo_tmp, $destino);

In any case, the most correct thing is to use the move_uploaded_file function to move an upload file.

So:

  if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino)) {
      echo "O seu arquivo foi movido com sucesso";
  }
    
17.08.2018 / 16:34
-1

Second manual

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "falha ao copiar $file...\n";
}
?>

Where: $file is the source file and $newfile is the destination file. If the destination file already exists, it will be overwritten.

You are getting the wrong value to pass as a source file.
So you get it $arquivo = $_FILES['arquivo']['name']

    
17.08.2018 / 16:34