move_uploaded_file only works once in the file

Asked

Viewed 424 times

2

I’m using the function move_uploaded_file

The first time I use it, I send a certain video to a temporary folder.

1

move_uploaded_file($_FILES['cadVideo']['tmp_name'], "temporario/" . 'temp-'.$_FILES['cadVideo']['name']);

Works perfectly!

Ok! After that, I do some other assignments and functions and again I need to use the move_uploaded_file. It turns out that at this second point it no longer works.

2

move_uploaded_file($_FILES['cadVideo']['tmp_name'], "../../Aulas/{$sessao[0]}/{$sessao[1]}/{$sessao[2]}/".$_FILES['cadVideo']['name']);

At first I thought it might be some error related to directories or something like that, so I reviewed it and concluded that this was not the error. But it still didn’t work.

For test purposes I copied and pasted the same function (1) and put in place the (2) and to my surprise it didn’t work either.

I was intrigued by this, how can a function that works normally not work again a few lines of code after?

move_uploaded_file works only once in the file? If yes, how can I dodge this problem?

  • Yes, it works only once, because once moved it is no longer uploaded file. To avoid this, move with the normal PHP file functions instead of using the uploads specific. or move to the right place at once. move_uploaded_file is practically an operation only to "accept definitively" the upload taking from the temporary location and avoiding its disposal.

  • And what are these normal functions? It is not possible to move to the right place at once, the file goes through small treatment before.

  • use normal "move" even (rename directory part) http://php.net/manual/en/function.rename.php

2 answers

2

What happens in this case is the following: When the POST is done by sending the file through the form, this file is saved in the server memory, and when you use the function move_uploaded_file It takes the file from memory and plays in the destination folder. That’s why when you try to use for the second time it no longer finds the file in memory as it has already been moved to a destination.

In this case you can simply copy the file you have already saved on disk to another location using the function copy PHP, see her official documentation here: http://php.net/manual/en/function.copy.php

Or if you want to move the file to another location, you can use the function rename PHP, which if another directory is specified it automatically moves the file. Official documentation: http://php.net/manual/en/function.rename.php

  • The copy in this case duplicates my file! And as it is only a temporary file there is a need to be destroyed later. But I liked the explanation of the function! Thank you.

  • In this case you can use the function rename to move the file. I added in the reply

2


The move_uploaded_file is practically an operation only to "accept definitively" the upload taking from the temporary location (usually the TEMP or TMP of the file system itself) avoiding its disposal at the end of the script. Once moved, it will be treated like any other file by PHP.

In short, you will only use the move_uploaded_file at first glance.

To move a conventional PHP file the function is rename, which is suitable for the second step of your application, after making the necessary treatment in the previously accepted file.

Example of use:

rename( "caminho1/arquivo.jpg", "caminho2/arquivo.jpg" );

The rename serves both to change the name of the file itself, and the path of this. To move without changing the name, just change only the part of the path, and keep the end. This is similar to what happens on the command line of *Nix-based systems

More in the manual:

http://php.net/manual/en/function.rename.php

Browser other questions tagged

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