php 上傳圖片改名

來源:互聯網
上載者:User
從網上找了一段上傳 圖片代碼,然後想把上傳的圖片名改成使用者名稱+圖片尾碼(使用者名稱為$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.';}}?>

搞定了,多謝

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.