使用move_uploaded_file在PHP中重命名上传的文件

时间:2014-12-22 23:21:24

标签: php forms file-upload

我有这段代码,想重命名上传的图片名称!

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadedfile" accept="image/*" capture>
    <input type="submit" value="Upload">
</form>

<?php 
    $target_path = "upload/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else {
        echo "There was an error uploading the file, please try again!";
    }
?>

我该怎么办?

1 个答案:

答案 0 :(得分:1)

使用move_uploaded_file($filename, $destination)时,您可以在目的地指定文件名。

$target_path = "upload/";
$target_filename = 'filename.xyz'
$target_path = $target_path . $target_filename); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".  $target_filename). 
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

比照move_uploaded_file($filename, $destination)