php - 用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);
}
这样你就不需要修改你的类,如果你修改你的类,你会在你的条件的“ELSE”方面遇到麻烦,这将导致额外的花费时间来获得扩展。
推荐阅读
- php - 在php中计算多维数组中的值
- c# - 替换列中的多个字符串元素
- python - 从python中的(2,MN)矩阵中读取(M,N)图像的值
- windows - Log4net 滚动文件具有旧的修改和访问时间戳
- pine-script - Pine Script 中一长串函数值的换行
- mysql - MySQL - 选择第一个字符是小写还是大写
- c# - 将数据表列转换为列表
- r - 使用 montecarlo 从乘法中获得分布
- json - Spark -scala 从 json 文件创建 DataFrame 并删除基本和嵌套元素
- android - 执行“com.android.support.appcompat-v7-28.0.0”更新实现“com.android.support.play-services-ads:17.0.0”时出错