您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 上传图片易名

2024/3/21 4:47:53发布32次查看
php 上传图片改名
从网上找了一段上传 图片代码,然后想把上传的图片名改成用户名+图片后缀(用户名为$user,)但是没看懂这段代码,所以不知道怎么弄,请各位指点
$user=hah;
$targetfolder = '/uploads'; // relative to the root
if (!empty($_files)) {
$tempfile = $_files['userfile']['tmp_name']; //
$targetpath = $_server['document_root'] . $targetfolder;
$targetfile = rtrim($targetpath,'/') . '/' . $_files['userfile']['name'];
$filetypes = array('jpg','jpeg','gif','png'); // file extensions
$fileparts = pathinfo($_files['userfile']['name']);
if (in_array($fileparts['extension'],$filetypes)) {
$arr = getimagesize($tempfile);
move_uploaded_file( $tempfile,$targetfile);
$data = array(
state => '00',
desc => 'ok',
src => http://localhost/uploads/.$_files['userfile']['name'],
touxiang => $touxiangs,
);
echo json_encode($data);
} else {
echo 'invalid file type.';
}
}

------解决方案--------------------
修改第7行
$targetfile = rtrim($targetpath,'/') . '/' . $_files['userfile']['name'];
------解决方案--------------------
$targetfile = $targetpath . '/' . $user . '.' . pathinfo($_files['userfile']['name'], pathinfo_extension);
------解决方案--------------------
'00',
desc => 'ok',
src => http://localhost/uploads/.$user . '.' .$fileparts['extension'],
touxiang => $touxiangs,
);
echo json_encode($data);
} else {
echo 'invalid file type.';
}
}
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product