How to use copy()?

Asked

Viewed 80 times

2

I’m creating a project to import files, only I need the file name to use "LOAD LOCAL DATA INFILE", before I searched for a way to read the temporary file automatically, until I saw this form of copy.

I am wanting 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 mistake to me: copy() expects parameter 1 to be a valid path, array given in

Just follow my code:

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

$arquivo_tmp = $_FILES['arquivo']['tmp_name'];
   copy($arquivo, $destino);
  • give a var_dump in $arquivo and $destino, it seems to me that one of these two is not with the expected value.

2 answers

1


This line here:

$arquivo = $_FILES['arquivo'];

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

If you want to get the client’s original 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 through the form, like this:

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

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

Thus:

  if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino)) {
      echo "O seu arquivo foi movido com sucesso";
  }
  • It worked here, but how would it look if I used the "move_uploaded file"? I had tried to use it too, only I was also giving error.

  • @Emanoel edited there. Take a look.

  • 1

    It was right. Thank you, man!!!

-1

According to manual

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

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

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

You are picking the wrong value to pass as source file.
Catch him like this $arquivo = $_FILES['arquivo']['name']

Browser other questions tagged

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