简体   繁体   English

PHP Move_Uploaded_Files重命名文件

[英]PHP Move_Uploaded_Files rename file

I'm trying to upload files through a jquery image uploader, but I'd like all files to use the variable 'fileName' to name the file that gets uploaded. 我正在尝试通过jquery图片上传器上传文件,但是我希望所有文件都使用变量'fileName'来命名要上传的文件。 As it works currently the file is uploaded using its original name. 当前可以正常使用的文件是使用其原始名称上传的。 Any suggestions? 有什么建议么?

<?php

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

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;

Simply change 只需更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM