Jump to content

file rename


magcr23

Recommended Posts

Hi guys, i'm trying to upload a file and while the name already exists it add a number (1 then 2 then 3....).

I made it work in one file, but now i switched the folder and i can't make it work.

There's the code:

<?php
$con=mysqli_connect("localhost","root","6794","website");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$categoria = $_POST["categoria"];
	
//INSERIR IMAGEM

$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
	
$i = 1;
$actual_name = pathinfo($name,PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo($name, PATHINFO_EXTENSION);
	
$nome = $actual_name.".".$extension;
$caminho = $name;

if($error > 0){
die("Erro");
}
else{
if($type == "imgcategoria/jpg" || $type == "imgcategoria/png" || $type == "image/jpeg"){
		
if(file_exists('image/' . isset($_FILES[$name]['name']))){
				
while(file_exists('imgcategoria/'.$actual_name.".".$extension)){           
$actual_name = (string)$original_name.$i;
$nome = $actual_name.".".$extension;
$i++;
}
				
echo "O upload do ficheiro " . $nome .  " deu certo.";
			
//mover ficheiro para pasta imgcategoria
move_uploaded_file($temp,"imgcategoria/".$nome);
			
//inserir caminho do ficheiro na base dados
$caminho2 = $nome;

$insere2 = "INSERT INTO imagem(id, caminho) VALUES (DEFAULT, '$caminho2')";
mysqli_query($con, $insere2);
				
$imgCat = "INSERT INTO categoria (id, nome, caminhoImagem) VALUES ( DEFAULT, '$categoria', '$caminho')";	
mysqli_query($con, $imgCat);
			
}
else{			
		
//mover ficheiro para pasta imgcategoria
move_uploaded_file($temp,"imgcategoria/".$name);
		
//inserir caminho do ficheiro na base dados
$caminho = $name;
	
$insereIMG = "INSERT INTO imagem(id, caminho) VALUES (DEFAULT, '$caminho')";
mysqli_query($con, $insereIMG);				
		
$imgCat2 = "INSERT INTO categoria (id, nome, caminhoImagem) VALUES ( DEFAULT, '$categoria', '$caminho')";	
mysqli_query($con, $imgCat2);	
		
}
}
else{
die("Tipo/tamanho nao suportado");
}		
}	

header('Location: inicio.php');
mysqli_close($con); 	
?>

The problem that i find is that when i have:

if($type == "imgcategoria/jpg" || $type == "imgcategoria/png" || $type == "image/jpeg"){
		
if(file_exists('image/' . isset($_FILES[$name]['name']))){

the upload works but it don't change the name.

 

if i use:

if($type == "imgcategoria/jpg" || $type == "imgcategoria/png" || $type == "imgcategoria/jpeg"){
		
if(file_exists('imgcategoria/' . isset($_FILES[$name]['name']))){

i get the error in 

die("Tipo/tamanho nao suportado");

and the upload don't work. 

 

Anyone know how to solve that?

Link to comment
https://forums.phpfreaks.com/topic/296883-file-rename/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.