簡體   English   中英

如何在PHP中重命名上傳的文件

[英]How to rename the uploaded files in PHP

extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {
    $file_name=$_FILES["files"]["name"][$key];
    $file_tmp=$_FILES["files"]["tmp_name"][$key];
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);

move_uploaded_file($file_tmp,"".$_POST['property_builder']."/".$_POST['property_city'].""."/".$_POST['property_location'].""."/".$_POST['property_type']."/".'Images-'.$_POST['property_name']."/".$file_name);


$content_to_write .= '<img src="Images-'.$_POST['property_name'].'/'.$file_name.'" alt="'.$file_name.'" title="'.$file_name.'" height="400" width="600">';
}

在上面的代碼中,我無法重命名上傳的文件.. 請幫助我如何按用戶重命名所有上傳的文件...

用戶,只需上傳 5 個文件,我想要 5 個文本框要求 5 個文件名.. 並相應地保存它們。

您應該更改以下代碼

move_uploaded_file($file_tmp,"".$_POST['property_builder']."/".$_POST['property_city'].""."/".$_POST['property_location'].""."/".$_POST['property_type']."/".'Images-'.$_POST['property_name']."/".$file_name);

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $_POST['filename']."."end($temp); // this filename is the name of field you have given in your text fields. If you have not given extension name you have to include that too.
// Make sure your path is currect where you're saving your file
move_uploaded_file($file_tmp,"./pathtomainimagefolder".$_POST['property_builder']."/".$_POST['property_city']."/".$_POST['property_location']."/".$_POST['property_type']."/".'Images-'.$_POST['property_name']."/".$newfilename);

只需確保文件路徑。 我只是根據你的路徑寫了 smaple。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM