doushu7588 2012-06-05 14:04
浏览 79
已采纳

使用随机名称将图像保存到数据库中

Good morning people, i've been trying to solve this for some days, but i must admit im kind of new on this of programming.

Actually im making a news website, and for the news management im having a small problem, i want that, when i click to save the pic it get's a random name, not the one it had from the user files, e.x: pic.jpg saves as 2613.jpg.

Im trying to do this to prevent that if there are 2 pics with same name the new one replace the old one, ive tried with javascript, php and yet i wouldnt be able to solve my problem, hope you guys may help me, by the way, im working using javascript, php, postgresql, jquery and ajax, if you need more details please let me know, and, once again, hope i can get some help from this community.

function guardar(){
if($('#titl').val() == '' || $('#categ').val() == '' || $('#cont').val()==''){
            alert("Todos los campos deben contener datos!");
            return false;
        }

        $.ajax({
            url: "noticias_php.php",
            data:{
                n : rand(),
                "accion" : "guardar",
                ID     : $('#id').val(),
                Titulo : $('#titl').val(),
                Categorias : $('#categ').val(),
                Contenido : $('#cont').val(),
                fecha: $('#fecha').val(),
                imagen: $('#img_id').val()

            },
            success: function(datos){
                var respuesta = explode("-*", datos);

                        if (respuesta[0] == "SI"){
                            aviso(respuesta[1]);
                            limpiar();
                        }else if (respuesta[0] == "NO"){
                            aviso(respuesta[1]);
                        }else{
                            aviso(datos);
                        }
            }
        });
    }
  • 写回答

4条回答 默认 最新

  • dsaf32131 2012-06-05 15:22
    关注

    There are a couple of things you can do here - I would recomend doing them at the server level using PHP. The code you display seems to be client side JavaScript.

    Typically you can generate a random number or datestamp (which may be more useful). Then either replace or append this to the file name. Depending upon the usage of your site both these should suffice.

    PHP random number generator - rand(0000, 9999)

    PHP datestamp generator (Note if generating a date don't include : characters etc these will break some image urls) - date("YmdHis")

    I often use some php like this to clean file names

    $file_name = str_replace(" ", "_", rand(0000, 9999)."_".eregi_replace("[[:space:]]+", " ", strtolower($_FILES['image']['name'])));
    

    This will not only make it unique by appending a 4 digit random number to the file name but will also stip all double spaces, make all lower case and also replace spaces with underscores. It might not the be the cleanest but it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开