php - Save an image into database with a random name -
good morning people, i've been trying solve days, must admit im kind of new on of programming.
actually im making news website, , news management im having small problem, want that, when click save pic get's random name, not 1 had user files, e.x: pic.jpg saves 2613.jpg.
im trying prevent if there 2 pics same name new 1 replace old one, ive tried javascript, php , yet wouldnt able solve problem, hope guys may me, way, im working using javascript, php, postgresql, jquery , ajax, if need more details please let me know, and, once again, hope can 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); } } }); }
there couple of things can here - recomend doing them @ server level using php. code display seems client side javascript.
typically can generate random number or datestamp (which may more useful). either replace or append file name. depending upon usage of site both these should suffice.
php random number generator - rand(0000, 9999)
php datestamp generator (note if generating date don't include : characters etc these break image urls) - date("ymdhis")
i use php clean file names
$file_name = str_replace(" ", "_", rand(0000, 9999)."_".eregi_replace("[[:space:]]+", " ", strtolower($_files['image']['name']))); this not make unique appending 4 digit random number file name stip double spaces, make lower case , replace spaces underscores. might not cleanest works.
Comments
Post a Comment