move file in php without replacing

-1

I am trying to move a file from php but without replacing it if it already exists.

The fact is that I'm not sure how to do it if there is more than one.

The code I'm using is this:

        $extension = explode(".", $_GET["foto"]);
        if (file_exists("../../Nuevas/".$Resultados['codigo'].".CR2")) { $nombrenuevo=$Resultados['codigo']."-1.CR2" } else { $nombrenuevo=$Resultados['codigo'].".CR2"; }
        if (rename ("../Fotos/cr2/".$extension[0].".CR2", "../../Nuevas/".$nombrenuevo)) {
        if (!unlink("../Fotos/jpg/".$_GET["foto"])) { $error = "Error Borrando ".$_GET["foto"]; }
        $ok = "el nombre del fichero ha sido cambiado\n";
} else {
        $error = "Se ha producido un error al intentar cambiar el nombre\n";
}

But in the case that there are more than two files with the same name, the second one is replaced. Is there any way to see if there is more than one?

Thank you very much for your help.

Possible solution that I have applied.

$extension = explode(".", $_GET["foto"]);
        $nombrenuevo=$Resultados['codigo'].".CR2";
        if (file_exists("../../Nuevas/".$nombrenuevo)) {
            for ($i=1; ; $i++){
                if (!file_exists("../../Nuevas/".$Resultados['codigo']."-".$i.".CR2")) { $nombrenuevo=$Resultados['codigo']."-".$i.".CR2"; break; }
            }
        if (rename ("../Fotos/cr2/".$extension[0].".CR2", "../../Nuevas/".$nombrenuevo)) {
        if (!unlink("../Fotos/jpg/".$_GET["foto"])) { $error = "Error Borrando ".$_GET["foto"]; http_response_code(500); }
        $ok = "el nombre del fichero ha sido cambiado\n";
} else {
        $error = "Se ha producido un error al intentar cambiar el nombre\n"; http_response_code(500);
}

At the moment I can not test if it works since I have a syntax error and I'm looking for the bug.

    
asked by Killpe 29.10.2017 в 13:57
source

1 answer

0

Having a files directory:

-->files |_archivo1.txt (lo llamaremos original) |_carpeta1 |_archivo1.txt (lo llamaremos copia) And the next approach

<?php /* 1° obtener lista de archivos del directorio de destino 2° comprobar si en esta lista existe un archivo con el mismo que nuestro original 3° en el caso de que no existe una copia mover el original */ ?> We could have this too: <?php /* 1° intentar obtener el archivo en el destino 2° en el caso de que no exista una copia mover el original */ ?> I bet on the second, since it is faster. And in part it's the one you use but you're doing an explode to get the file extension. What happens if the file contains several points?

Ej: mi.archivo.txt.doc.txt

The extension would not be valid and therefore the rest of the process either.

The best option is to treat the whole name. This is a very basic example:

<?php $destino = 'files/carpeta1/'; $origen = 'files/archivo1.txt'; $nombreArchivo = end(explode('/', $origen)); $pathFinal = $destino . $nombreArchivo; if(!file_exists($pathFinal)) { try { rename($origen , $pathFinal); unlink($origen); } catch($error) { exit($error); } } ?>

Update

The use of the glob function allows you to consult all the files that meet a pattern. This helps us filter by our employer to make a count and put an appropriate copy file number.

Here is the official documentation link

    
answered by 29.10.2017 в 15:54