PHP我想在文件上传文件重命名,看过您原来的回答
这个PHP上传中文文件名时,会有乱码或传不上问题,我想到了重命名。看过您原来的回答,但是作为美工实在是有点深奥,不知道加在什么位置。能否帮忙修改一下?谢谢了。<?php/...
这个PHP上传中文文件名时,会有乱码或传不上问题,我想到了重命名。
看过您原来的回答,但是作为美工实在是有点深奥,不知道加在什么位置。
能否帮忙修改一下?谢谢了。
<?php
// If you want to ignore the uploaded files,
// set $demo_mode to true;
$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg','png','gif','bmp');
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){
// File uploads are ignored. We only log them.
$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.');
}
// Move the uploaded file from the temporary
// directory to the uploads folder:
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!');
// Helper functions
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);
}
?> 展开
看过您原来的回答,但是作为美工实在是有点深奥,不知道加在什么位置。
能否帮忙修改一下?谢谢了。
<?php
// If you want to ignore the uploaded files,
// set $demo_mode to true;
$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg','png','gif','bmp');
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){
// File uploads are ignored. We only log them.
$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.');
}
// Move the uploaded file from the temporary
// directory to the uploads folder:
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!');
// Helper functions
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);
}
?> 展开
1个回答
展开全部
if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
要达到你说的功能,可以修改上面这句代码。不知道你要的重命名是需要什么格式的,还有确认下修改了保存的文件名后是否对其他地方有影响。
你说的文件名会乱码是因为上传的文件的文件名有中文,可能会导致的乱码。这里做一下转换应该就没有问题了。
追问
请问应该怎么改呢,比如按时间时间后面几个随机数字什么的,只要名字不重复就可以。
我是做美工的这个对我太深奥了。
还请麻烦您帮我写一下,谢谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询