从网上找了一段上传 图片代码,然后想把上传的图片名改成用户名+图片后缀(用户名为$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.';
}
}
?>
