在 PHP 上传文件?

Upload file in 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'];
}

根据你的情况。你可以通过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);
    }  

这样你就不需要修改你的 class,而且如果你修改你的 class 你会在你的条件 "ELSE" 方面遇到麻烦,这会导致额外的支出获得延期的时间。