drcj64241 2014-04-10 11:45
浏览 13
已采纳

编码重音和特殊字符PHP

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!

  • 写回答

3条回答 默认 最新

  • douba4824 2014-04-10 11:54
    关注

    Apart from the utf-8 issue already covered nicely by Amauri, another thing you might want to do is this:

    replace

    $ruta ="tmp/".$_FILES["file"]["name"];
    $rutafinal =str_replace(" ","_",$ruta); //Change " " for "_"
    

    with

    $ruta      = "tmp/".$_FILES["file"]["name"];
    $fileExtension = substr($ruta,stripos ($ruta,'.'));
    $rutafinal = 'tmp/' . md5(time() . $ruta).'.'.$fileExtension;
    

    This will create a md5 hash from the current timestamp and your filename as your new filename, thus giving you a better chance that your filename is unique on your system plus getting rid of "special chars".

    One thing i like to do is also use the users unique id (if that exists) as an addition to prevent problems when two users upload the same file (might happen a lot with filenames like avatar.jpeg and such).

    After all, md5 is not collision free so you might have to do some extra work here. One way to up your chances is to use timestamp driven directories as well:

    $rutafinal = 'tmp/' . date("Y/m/d/h/i", time()) . '/'. md5(time() . $ruta).'.'.$fileExtension;
    

    This would, as an example, put your file in

    <yourimageuploadfolder>/2014/04/10/14/04/5eb63bbbe01eeed093cb22bb8f5acdc3.jpg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输