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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测