【问题标题】:Rename uploaded file in PHP using move_uploaded_file使用 move_uploaded_file 在 PHP 中重命名上传的文件
【发布时间】:2014-12-22 23:21:24
【问题描述】:

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

<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!";
    }
?>

我能做什么?

【问题讨论】:

标签: php forms file-upload


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 2015-06-09
    • 1970-01-01
    • 2015-02-05
    • 2020-05-19
    • 2011-11-30
    • 2016-02-28
    • 2013-09-21
    相关资源
    最近更新 更多