move_uploaded_file only works once in the file

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!

Okay! After that, I perform some other assignments and functions and again need to use move_uploaded_file . It turns out that at this second point it does not work anymore.

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 directory-related error or something, so I reviewed it and found that it was not the error. But it still did not work.

For testing purposes I copied and pasted the same (1) role and put it in the place of (2) and to my surprise it did not work either.

>

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

Does move_uploaded_file work only once in the file? If so, how can I avoid this problem?

    
asked by anonymous 25.08.2017 / 18:34

2 answers

2

move_uploaded_file is practically an operation only to "accept in definitive" the upload by taking out of the temporary location (usually the TEMP itself or TMP of the file system) avoiding its discarding at the end of script . Once moved, it will be treated as another file by PHP.

In short, you will only use move_uploaded_file at first.

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

Example usage:

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

The rename serves both to rename the file itself, and its path. To move without renaming, just change the part of the path, and keep the ending. This is similar to what happens at the command line of * nix-based systems

More in the manual:

  

link

    
25.08.2017 / 18:49
2

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

In this case you can simply copy the file you have already saved to disk to another location using the copy function of PHP, see the official documentation of it here: link

Or if you want to move the file to another location, you can use the rename function of PHP, which if another directory is specified it automatically moves the file. Official documentation: link

    
25.08.2017 / 18:45