SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question is there a better way upload photo

    i have this form which you fill in and attach a photo this then writes the info to database with a temp image name
    then when i get the photo in email i rename the photo and put it in a image folder and edit the table with new name
    what i was thinking is there a way that when the form is filled in it sends the info to database without the photo info
    then on another page they submit a photo and it gets renamed to pic2 but the number is assigned a number and the photo is sent to the image folder
    hope you can make sense of this
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  2. #2
    SitePoint Addict joaquin_win's Avatar
    Join Date
    Jul 2005
    Location
    Venezuela
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hope you can make sense of this
    not too much :P

    I think you want to take a look at php's move_uploaded_file and is_uploaded_file

    Those will allow you rename the uploaded file before saving its info to the db.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have an ID field in your register table and it is auto increment, then you could use it to rename the photo.

    PHP Code:
    $query "
       INSERT INTO register (field1, field2, field3)
       VALUES('
    $value1', '$value2', '$value3')
     "
    ;
     
    $result mysql_query($query,$link);
     
    $photoid mysql_insert_id($link);

     
    move_uploaded_file($_FILES['userfile']['tmp_name'], 'path/to/your/uploads/pic'.$photoid.'.jpg'); 

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    If the images will be going into the same directory then you should really consider using the primary key as the name of the image. You could further enhance the speed and efficency by using a set character string for that primary key column rather then a variable length one. The last thing I would recommend is inserting the image into the database before it has been safely saved to the directory. If you save the image to the database before then making the assumption that it will be moved without error which is a very poor assumption to make.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    This function will generate a relatively unique file name as a 18 character string. Just call this function and set the file name to this return value with the extension. Then store the return value of this method separately from the extension in the database. Then you can query the database rto see if the file name already exists before actually saving the item as that name.

    PHP Code:
    function makeUniqueFileName() {

        
    $ipbits explode("."$_SERVER["REMOTE_ADDR"]);
        list(
    $usec$sec) = explode(" ",microtime());
        
    $usec = (integer) ($usec 65536);
          
    $sec = ((integer) $sec) & 0xFFFF;
          
    $uid sprintf("%08x_%04x_%04x",($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec$usec);
          return 
    $uid;



  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry. I should have made my field names a little less general. I wasn't suggesting to save images into a database.

    PHP Code:
    $query "
       INSERT INTO register (fname, lname, email)
       VALUES('
    $value1', '$value2', '$value3')
     "
    ;
     
    $result mysql_query($query,$link);
     
    $photoid mysql_insert_id($link); 
    This way the registrant's id is related to the image.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for all replies
    do i have to have 2 forms
    1 for all details name email etc to upload to database
    2to upload photos to a folder on server
    have seen scritp to upload photo but havnt seen one for both
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  8. #8
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. You can have all the fields on one form. Just be sure to include the 'enctype' attribute as part of the form tag in the form containing the file upload field. Also include the MAX_FILE_SIZE anywhere before the file upload field.

    HTML Code:
    <FORM action="your.php" method="post" enctype="multipart/form-data">
    <INPUT type="hidden" name="MAX_FILE_SIZE" value="5250000"><!-- max 5.25MB -->

  9. #9
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know maybe your system is already good the way it is, then you can approve any photo that is uploaded before it's posted on the site. It doesn't really take too much time.


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
  •