PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php 上传图片易名

原创
2016-06-13 11:47:02 527浏览

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,'//m.sbmmt.com/m/') . '//m.sbmmt.com/m/' . $_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,'//m.sbmmt.com/m/') . '//m.sbmmt.com/m/' . $_FILES['userfile']['name'];

------解决方案--------------------
$targetFile = $targetPath . '//m.sbmmt.com/m/' . $user . '.' . pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
------解决方案--------------------



$user="hah";
$targetFolder = '/uploads'; // Relative to the root

if (!empty($_FILES)) {
$tempFile = $_FILES['userfile']['tmp_name']; //
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;


$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['userfile']['name']);

$targetFile = rtrim($targetPath,'//m.sbmmt.com/m/') . '//m.sbmmt.com/m/' . $user . '.' .$fileParts['extension'];
if (in_array($fileParts['extension'],$fileTypes)) {
$arr = getimagesize($tempFile);



move_uploaded_file( $tempFile,$targetFile);
$data = array(
"state" => '00',
"desc" => 'ok',
"src" => "http://localhost/uploads/".$user . '.' .$fileParts['extension'],
"touxiang" => $touxiangs,
);


echo json_encode($data);
} else {
echo 'Invalid file type.';
}
}





?>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。