繁体   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