将上传图像的名称更改为特定名称

时间:2013-07-29 09:08:32

标签: php

我有上传图片上传表单,我想将图片名称更改为特定名称。

这是我的代码:

<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

if(!in_array(strtolower($extension), $allowed)){
    echo '{"status":"error"}';
    exit;
}

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
    echo '{"status":"success"}';
    exit;
}
}

echo '{"status":"error"}';
exit;

3 个答案:

答案 0 :(得分:0)

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/your_own_name_here.'$extension)){
    echo '{"status":"success"}';
    exit;
}

答案 1 :(得分:0)

您应该在以下代码中更改 $ _ FILES ['upl'] ['name']

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){

例如:

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/nameOfTheFile.jpg')){

答案 2 :(得分:0)

您可以在调用函数move_uploaded_file中添加自定义文件名。 等,

 if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.'yourcustomname'.$extension )){

如果您想添加唯一名称,则可以使用like,

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.time().$extension )){