如我們在上傳檔案頁面中如下操作
代碼如下 |
複製代碼 |
$name=$_FILES['userfile']['name']; //將所上傳的檔案名稱賦予name
|
然後我們要對它進行重新命名
代碼如下 |
複製代碼 |
//檔案名稱的自動產生 list($usec, $new_name) = explode(" ", microtime()); if(@$extension_name){//限制檔案類型為jpg、gif、png、bmp move_uploaded_file($tmp_name,$new_dir.'/'.$new_name.'.'.$extension_name);//移動到目標檔案夾 echo '上傳成功'; |
這樣我們就把檔案名稱以時間日期產生了一個新的檔案名稱了
例
代碼如下 |
複製代碼 |
<form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> |
以下是我添加的將檔案名稱重新命名為系統時間的代碼:
代碼如下 |
複製代碼 |
<?php
$date = date(ymdhis); //echo($date); if ((($_FILES["uppic"]["type"] == "image/gif") || ($_FILES["uppic"]["type"] == "image/jpeg") || ($_FILES["uppic"]["type"] == "image/pjpeg")) && ($_FILES["uppic"]["size"] < 2000000)) { if ($_FILES["uppic"]["error"] > 0) { echo "Return Code: " . $_FILES["uppic"]["error"] . "<br />"; } else { //echo "Upload: " . $_FILES["uppic"]["name"] . "<br />"; //echo "Type: " . $_FILES["uppic"]["type"] . "<br />"; //echo "Size: " . ($_FILES["uppic"]["size"] / 1024) . " Kb<br />"; //echo "Temp file: " . $_FILES["uppic"]["tmp_name"] . "<br />"; $uptype = explode(".", $_FILES["uppic"]["name"]); $newname = $date.".".$uptype[1]; //echo($newname); $_FILES["uppic"]["name"] = $newname; if (file_exists("pics/" . $_FILES["uppic"]["name"])) { echo $_FILES["uppic"]["name"] . " already exists. "; } else { $_FILES["uppic"]["name"] = move_uploaded_file($_FILES["uppic"]["tmp_name"], "pics/" . $_FILES["uppic"]["name"]); echo "<script>alert('上傳成功!')</script>"; echo "<script>window.location.href='http://www.111cn.net';</script>"; } } } else { echo "Invalid file"; } ?> |
好了,就講到這裡了,大家可參考。