为上传的图像提供mysqli的唯一名称

时间:2016-05-29 22:35:48

标签: php file-rename

我希望允许用户上传图片而不会出现可能由多个用户上传可能具有相同图片名称的图片而导致的冲突问题。我很难理解如何执行此操作,我不知道从哪里开始......

这是我的代码:

 if(isset($_POST['submitimage'])){
                move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$_FILES['file']['name']);
                $con = mysqli_connect("localhost","root","","database");
                $q = mysqli_query($con,"UPDATE users SET image = '".$_FILES['file']['name']."' WHERE user_id = '".$_SESSION['user']."'");
                 header("Location: index.php");
        }
?>

任何帮助都会很棒。谢谢!

3 个答案:

答案 0 :(得分:2)

我的解决方案是为每个上传的文件生成一个随机字符串,即:

<?php
   if(!empty($_POST['submitimage'])){
                //get file extension.
                $ext = pathinfo($_FILES['file']['name'])['extension'];
                //generate the new random string for filename and append extension.
                $nFn = generateRandomString().".$ext";
                move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$nFn);
                $con = mysqli_connect("localhost","root","","database");
                $q = mysqli_query($con,"UPDATE users SET image = '{$nFn}' WHERE user_id = '{$_SESSION['user']}'");
                 header("Location: index.php");
        }

function generateRandomString($length = 10) {
    return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, $length);
}

?>

答案 1 :(得分:1)

PHP具有内置函数,可在服务器上生成唯一文件。此功能称为tempnam()。如果您仔细阅读该网站上的评论,那么如果许多进程同时调用该函数,您很可能会从该函数中获得不需要的行为。因此对此功能的修改如下:

<?php

function tempnam_sfx($path, $suffix){
    do {
        $file = $path."/".mt_rand().$suffix;
        $fp = @fopen($file, 'x');
    }
    while(!$fp);

    fclose($fp);
    return $file;
}

?>

由于文件在创建时保持打开状态,因此无法通过其他进程访问该文件,因此无法创建具有相同名称的2个文件只是因为一对您的网站访问者恰好在同一时刻上传图片。所以要在你自己的代码中实现它:

<?php

function tempnam_sfx($path, $suffix){
    do {
        $file = $path."/".mt_rand().$suffix;
        $fp = @fopen($file, 'x');
    }
    while(!$fp);

    fclose($fp);
    return $file;
}

$uploaddir = 'pictures'; // Upload directory
$file = $_FILES['file']['name']; // Original file
$ext = pathinfo($path, PATHINFO_EXTENSION); // Get file extension

$uploadfile = tempnam_sfx($uploaddir, $ext);

move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
$con = mysqli_connect("localhost","root","","database");
$q = mysqli_query($con,"UPDATE users SET image = '".basename($uploadfile)."' WHERE user_id = '{$_SESSION['user']}'");
header("Location: index.php");

?>

答案 2 :(得分:0)

你可以做到这一点的一种方法是生成一些随机数(并可能以数字格式将它们附加到当前日期)并为图像提供数字序列。

 if(isset($_POST['submitimage'])){
            //generate 3 sequences of random numbers,you could do more or less if you wish
            $randomNumber=rand().rand().rand();
            move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$randomNumber."jpg");
            $con = mysqli_connect("localhost","root","","database");
            $q = mysqli_query($con,"UPDATE users SET image = '".$randomNumber.".jpg' WHERE user_id = '".$_SESSION['user']."'");
             header("Location: index.php");
    }
?>

注意:如果数字不是你的话,你也可以查看generating random strings