2016-08-05 85 views
0

我曾嘗試以下方式上傳文件,但文件沒有從我的目標文件夾上傳,我已經和重命名文件..其實「filebutton」是我輸入的元素名稱掙扎..如何在將文件上傳到服務器之前進行重命名?

我上傳腳本

$target_dir = "../resumes/"; 
        $file_name = $cand_arrval['resume_title'].$cand_arrval['resume_name']; 
        $filename = basename($_FILES["filebutton"]["name"]); 
        $filename = $file_name . strrchr($filename, '.'); 
        $target_file = $target_dir .$filename; 

       $uploadOk = 1; 
       echo $FileType = pathinfo($target_file,PATHINFO_EXTENSION); 

       $selector = ""; 
       // Check if file already exists 
       if (file_exists($target_file)) { 
        $uploadOk = 0; 

       } 
       // Check file size 
       if ($_FILES["filebutton"]["size"] > 500000) { 
        $uploadOk = 0; 

       } 
       // Allow certain file formats 
       if($FileType != "doc" && $FileType != "docx" && $FileType != "txt") { 
        $uploadOk = 0; 

       } 
       // Check if $uploadOk is set to 0 by an error 
       if ($uploadOk == 0) { 
        $prop->call_rollback(); 

       // if everything is ok, try to upload file 
       } else { 
        $temp = explode(".", $_FILES["filebutton"]["name"]); 
        $extension = end($temp); 


        if (move_uploaded_file($_FILES["filebutton"]["tmp_name"], $target_file)) { 
         echo "<center><div class='bg bg-success text-success' style='padding:5px;'>The file <b><i>". basename($_FILES["filebutton"]["name"]). "</i></b> has been uploaded.!</div></center>"; 
        } else { 
         echo "<div class='bg bg-danger text-warning' style='padding:5px;'>Sorry, there was an error uploading your file.</div>"; 
        } 
       } 

$file_name實際上是我的重命名文本。如果有任何簡單的方法建議我重命名原始文件名...

+0

你嘗試'$ TARGET_FILE = $ TARGET_DIR $ FILE_NAME;' –

+0

是我沒有這樣的文件名改變,但文件存儲在文件夾'rename'但我想店文件擴展名爲「rename.docx」 –

回答

0

您應該能夠添加所需的文件擴展名.docx當您重命名該文件,只要你還沒有一個文件夾在那個名字的位置。

$target_file = $target_dir . "$file_name.docx"; 
0

試試這個

$allowed_file_types = array ('doc', 'docx', 'txt'); 

if (!in_array($extension, $allowed_file_types) || $_FILES["filebutton"]["size"] > 500000) { 
$prop->call_rollback(); 

} else { 

    ..... 
} 
相關問題