I have a little problem with me php to upload imgs.
I'm making a website where a user may view a preview of the image that goes up, but if the image contains accents goes wrong such as "Nenúfares.jpg" becomes "Nenúfares.jpg.jpg"
How I can fix this?
Had thought about putting some method to select when an alert comes in. (JS) as containing special characters such as accents, etc ...
Mi Code
PHP:
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
$ruta ="tmp/".$_FILES["file"]["name"];
$rutafinal =str_replace(" ","_",$ruta); //Change " " for "_"
move_uploaded_file($_FILES["file"]["tmp_name"],$rutafinal);
echo $rutafinal;
}
else
{
echo "Archivo no valido";
}
JavaScript (AJAX):
function previewIMG() {
var formData = new FormData();
var file = $("#fileselectInput")[0].files[0];
formData.append("file", file);
formData.append("name", file.name);
var url ="subir_img_producto.php";
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var http = new XMLHttpRequest();
http.open("POST", url, true);
http.send(formData);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
var result= http.responseText;
var comprobar= 'tmp';
if (result.indexOf(comprobar) !== -1) {
document.getElementById('cuadroIMG').src = http.responseText;
}else{
alert(http.responseText);
}
}
}
}
HTML:
<form id="form">
<input type="file" id="fileselectInput" onchange="previewIMG()"><br>
</form>
Thank you very much!