After renaming the php Upload image, I found an upload & nbsp; image code from the internet. then I want to change the name of the uploaded image to the user name + image suffix (the user name is $ user ,) but I don't understand this code, so I don't know how to do it. please give us $ userhah; $ targetFolder & nbsp; uploads; & nbsp; Relative & nbsp; to & nbsp; the & nbs php Upload Image renamed
I found a piece of Upload image code from the Internet, and then I want to change the name of the uploaded image to the user name + image suffix (the user name is $ user). but I didn't understand the code, so I don't know how to get it, please advise
$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.';
}
}
------ Solution --------------------
Modify row 7th
$ TargetFile = rtrim ($ targetPath, '/'). '/'. $ _ FILES ['userfile'] ['name'];
------ Solution --------------------
$ TargetFile = $ targetPath. '/'. $ user. '.'. pathinfo ($ _ FILES ['userfile'] ['name'], PATHINFO_EXTENSION );
------ Solution --------------------
$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,'/') . '/' . $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.';
}
}
?>