Php easy name for uploading images

Source: Internet
Author: User
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.';
}
}





?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.