本文介绍了如何在php中更改上传文件的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来上传 jpg 文件.我需要将某人上传的文件重命名为 newtexture.jpg'.

I have this code to upload jpg files. I need to rename the file that someone uploads to newtexture.jpg'.

<?php
$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg');

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status('Error! Wrong HTTP method!');
}

if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){
    $pic = $_FILES['pic'];
    if(!in_array(get_extension($pic['name']),$allowed_ext)){
        exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
    }
    if($demo_mode){
        $line = implode('       ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
        file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
        exit_status('Uploads are ignored in demo mode.');
    }
    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
        exit_status('File was uploaded successfuly!');
    }
}
exit_status('Something went wrong with your upload!');

function exit_status($str){
    echo json_encode(array('status'=>$str));
    exit;
}

function get_extension($file_name){
    $ext = explode('.', $file_name);
    $ext = array_pop($ext);
    return strtolower($ext);
}
?>

附注.它说它主要是编码以便添加更多细节?还有什么……我是一名学生,我的 Capstone 项目需要它,这是紧急情况……

PS. It says that it is mostly coded so to add more details? What else... I am a student and I need it for my Capstone project and it is an emergency...

推荐答案

move_uploaded_file() 是新文件名,因此您只需将其更改为您想要的名称即可.

The second argument in move_uploaded_file() is the new filename, so all you need to do is change it to what you want.

if(move_uploaded_file($pic['tmp_name'], $upload_dir.'newtexture.jpg')){

这篇关于如何在php中更改上传文件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:18