javascript - Encode accents and special characters PHP -
i have little problem me php upload imgs.
i'm making website user may view preview of image goes up, if image contains accents goes wrong such "nenúfares.jpg" becomes "nenúfares.jpg.jpg"
how can fix this?
had thought putting method select when alert comes in. (js) containing special characters such 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 " " "_" 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 much!
apart utf-8 issue covered nicely amauri, thing might want this:
replace
$ruta ="tmp/".$_files["file"]["name"]; $rutafinal =str_replace(" ","_",$ruta); //change " " "_"
with
$ruta = "tmp/".$_files["file"]["name"]; $fileextension = substr($ruta,stripos ($ruta,'.')); $rutafinal = 'tmp/' . md5(time() . $ruta).'.'.$fileextension;
this create md5 hash current timestamp , filename new filename, giving better chance filename unique on system plus getting rid of "special chars".
one thing use users unique id (if exists) addition prevent problems when 2 users upload same file (might happen lot filenames avatar.jpeg , such).
after all, md5 not collision free might have work here. 1 way chances use timestamp driven directories well:
$rutafinal = 'tmp/' . date("y/m/d/h/i", time()) . '/'. md5(time() . $ruta).'.'.$fileextension;
this would, example, put file in
<yourimageuploadfolder>/2014/04/10/14/04/5eb63bbbe01eeed093cb22bb8f5acdc3.jpg
Comments
Post a Comment