首页 > 解决方案 > 用PHP上传文件?

问题描述

当我将图像文件上传到主机时,为什么文件再次重复文件扩展名?我检查主机中的文件名就像test.jpg.jpg

这是代码:

$uploadPath = "../../upload/Image/";

$handle = new Upload($_FILES['pic'], 'zh_TW');    

if($handle->uploaded){
    $pic=$_FILES['pic']['name'];
    if($pic != ''){
        $filename = $pic;
    }
    else{
        $microSecond = microtime();
        $filename = substr($microSecond, 11, 20).substr($microSecond, 2, 8);
    }                

    $handle->file_new_name_body     = $filename;
    $handle->image_resize           = true;
    $handle->image_ratio_y          = true;
    $handle->image_x                = 212;
    $handle->image_ratio_fill       = true;
    $handle->allowed                = array('image/*');
    $handle->file_overwrite         = true;
    $handle->process($uploadPath);
    if($handle->processed){ 
        $handle->file_dst_name = $filename;
    }
    else{
        echo "<script>";
        echo "alert('$handle->error');";
        echo "history.back();";
        echo "</script>";
        die;
    }
}
else{
    $filename = $_POST['currentPic'];
}

标签: phpupload

解决方案


根据你的条件。您可以通过explode 函数仅获取名称的一部分。

if($handle->uploaded){
    $pic=$_FILES['pic']['name'];
    if($pic != ''){
        //$filename = $pic;
        $filename = explode(".",$pic)[0];
    }
    else{
        $microSecond = microtime();
        $filename = substr($microSecond, 11, 20).substr($microSecond, 2, 8);
    }  

这样你就不需要修改你的类,如果你修改你的类,你会在你的条件的“ELSE”方面遇到麻烦,这将导致额外的花费时间来获得扩展。


推荐阅读