SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding generated random number to image name before upload.

    Hi,

    I have a cms that uploads data + images to a MySQL database, and I'm trying to add a random number that is generated to the image name before its uploaded.

    This is what I have but I'm struggling to add $random1 to the correct bit of the code for it to work.

    PHP Code:
    $random1 substr(number_format(time() * rand(),0,'',''),0,10);

    $target "imgdata/stock/";
    $target $target basename$_FILES['image1']['name']);
    $pic1=($_FILES['image1']['name']);

    $q=mysql_query("insert into stock (stock_Image) values('imgdata/stock/$pic1')") or die (mysql_error());

    move_uploaded_file($_FILES['image1']['tmp_name'], $target); 
    I have tried to add $random1 all over those scripts and havent worked it out.

    Thanks

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    As I always say echo out the variables as you go to see what they contain as this can give you an indication of what is happening.

    You will need to get the extension from somewhere but I would guess you want something along these lines:
    PHP Code:
    $random1 substr(number_format(time() * rand(),0,'',''),0,10); 

    $pic1$random1 $_FILES['ext']); // I do not know if there is an ext option in the $files array though!

    $target "imgdata/stock/" $pic1

    $q=mysql_query("insert into stock (stock_Image) values($target)") or die (mysql_error());
     
    move_uploaded_file($_FILES['image1']['tmp_name'], $target); 

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Rubble, that worked perfectly.

    I have a different script for when the image needs to be changed so will use your method and work it into that.

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •