我有这个代码来上传jpg文件。我需要将有人上传的文件重命名为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);
}
?>
PS。它说它主要是编码所以添加更多细节?还有什么......我是学生,我需要为我的Capstone项目而且这是一个紧急情况......
答案 0 :(得分:4)
move_uploaded_file()中的第二个参数是新文件名,因此您需要做的就是将其更改为您想要的内容。
if(move_uploaded_file($pic['tmp_name'], $upload_dir.'newtexture.jpg')){
答案 1 :(得分:1)
将您的文件名作为move_uploaded_file()
中的第二个参数传递$fileName = 'newtexture.jpg';
if(move_uploaded_file($pic['tmp_name'], $upload_dir.$fileName)){
答案 2 :(得分:1)
move_uploaded_file需要两个参数:
bool move_uploaded_file ( string $filename , string $destination )
因此,您必须指定第二个参数(路径+新文件名)。 E.g。
move_uploaded_file($pic['tmp_name'], $upload_dir.'newtexture.jpg')