PHP在上传之前重命名图像fime名称

时间:2016-03-10 23:13:55

标签: php file-rename

所以我想在推送到DB和指定文件夹之前,在原始文件名的前面添加一个随机数和一个_。

我整晚都在寻找互联网,无法找到我的解决方案

继承我的代码

这非常适合将我的文件上传到数据库和文件夹,但如果你在ios设备上拍照,那么文件总是名为image.jpg

$images_arr = array();

$target_dir = "uploads/";
$target = $target_dir.$_FILES['photo']['name'];

    //$target = $target . basename( $_FILES['photo']['name']);

    //This gets all the other information from the form
$userId=$_POST['userId'];
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];


    // Connects to your Database
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;

    //Writes the information to the database
mysql_query("INSERT INTO portal_phocagallery (user_id,title,alias,filename,description)
VALUES ('$userId', '$name', '$bandMember', '$pic', '$about')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES ['photo']['tmp_name'], $target)){

        //Tells you if its all ok
    echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory";
    $images_arr[] = $target;
}
else {

    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}


function errorMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}

function successMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}

谢谢你提前!

通过将前几行代码更改为此

,我完美地完成了工作
$images_arr = array();
            //This is the directory where images will be saved
        $target_dir = "uploads/";
        $file = rand().'_'.$_FILES['photo']['name'];
        $target = $target_dir.$file;

            //$target = $target . basename( $_FILES['photo']['name']);

            //This gets all the other information from the form
        $userId=$_POST['userId'];
        $name=$_POST['nameMember'];
        $bandMember=$_POST['bandMember'];
        $pic=$file;
        $about=$_POST['aboutMember'];

1 个答案:

答案 0 :(得分:0)

重新命名上传文件,当它从临时文件夹移动时通过$ new_target变量而不是$ target

$images_arr = array();

$target_dir = "uploads/";
$target = $target_dir.$_FILES['photo']['name'];
$pic = rand()."_".$_FILES['photo']['name'];
$new_target = $target_dir.$pic;

//$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$userId=$_POST['userId'];
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
//$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];


// Connects to your Database
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO portal_phocagallery (user_id,title,alias,filename,description)
VALUES ('$userId', '$name', '$bandMember', '$pic', '$about')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES ['photo']['tmp_name'], $new_target)){

    //Tells you if its all ok
    echo "The file ". $new_target. " has been uploaded, and your information has been added to the directory";
    $images_arr[] = $new_target;
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}


function errorMessage($str) {
    return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}

function successMessage($str) {
    return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}