magcr23 Posted June 17, 2015 Share Posted June 17, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/296883-file-rename/ Share on other sites More sharing options...
Solution requinix Posted June 18, 2015 Solution Share Posted June 18, 2015 (edited) Apparently you figured it out already, but those $types need to all use "image/". Edited June 18, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/296883-file-rename/#findComment-1514216 Share on other sites More sharing options...
magcr23 Posted June 18, 2015 Author Share Posted June 18, 2015 yeah i did it, i was using the replace tool of notepad++ and didn't noticed that xD. It's working already but thx anyway Quote Link to comment https://forums.phpfreaks.com/topic/296883-file-rename/#findComment-1514221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.