從網上找了一段上傳 圖片代碼,然後想把上傳的圖片名改成使用者名稱+圖片尾碼(使用者名稱為$user,)但是沒看懂這段代碼,所以不知道怎麼弄,請各位指點
$user="hah";$targetFolder = '/uploads'; // Relative to the rootif (!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.';}}?>
搞定了,多謝