SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing filenames of Multiple uploaded images

    I want to store filenames of all the 4 images uploaded via form into my MySQL table.

    here is my script

    PHP Code:
     $max_no_img=4// Maximum number of images value to be set here
                            
    for($i=1$i<=$max_no_img$i++){
                            echo 
    "<label>Add Property Image $i</label>
                            <input type=file name='cons_image[]' class='bginput'>"
    ;
                            }

     while(list(
    $key,$value) = each($_FILES['cons_image']['name']))
    {
    if(!empty(
    $value)){   // this will check if any blank field is entered
        
        
    $size=getimagesize($_FILES['cons_image']['tmp_name'][$key]);
       
    $sizekb=filesize($_FILES['cons_image']['tmp_name'][$key]);
       
    //reads the name of the file the user submitted for uploading
       
    $image=$_FILES['cons_image']['name'][$key];
       
    // if it is not empty


       // get the original name of the file from the clients machine
       
    $filename stripslashes($_FILES['cons_image']['name'][$key]);

       
    // get the extension of the file in a lower case format
       
    $extension getExtension($filename);
       
    $extension strtolower($extension);
       

       
           
    $randrand(0100000000);
          
    //we will give an unique name, for example a random number
          
    $image_name=$rand.'.'.$extension;
          
          
    //the new name will be containing the full path where will be stored (images folder)
          
    $consname="image/".$image_name;  //change the image/ section to where you would like the original image to be stored
          
    $consname2="image/thumb".$image_name//change the image/thumb to where you would like to store the new created thumb nail of the image
          
    $copied copy($_FILES['cons_image']['tmp_name'][$key], $consname);
          
    $copied copy($_FILES['cons_image']['tmp_name'][$key], $consname2);
          
         
    $sql="INSERT INTO property_list SET
          prop_name='
    $property',
          prop_size='
    $size',
          prop_location='
    $selectArea',
          prop_rent='
    $rent',
          date_added='
    $date',
          image_filename= '
    $image_filename[0]',
          image_filename2= '
    $image_filename[1]',
          image_filename3= '
    $image_filename[2]',
          image_filename4= '
    $image_filenames[3]'";
          
    $query mysql_query($sql)or die(mysql_error());
          
    }

    My problem is how to get the filenames from the array and put in the follwowing fields: image_filename, image_filename2, image_filename3, image_filename4

    hope someone can help

    cheers

  2. #2
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by parkerproject View Post
    My problem is how to get the filenames from the array and put in the follwowing fields: image_filename, image_filename2, image_filename3, image_filename4

    hope someone can help

    cheers
    No you have three problems.

    Rand will not work (even when called with large numbers) to produce unique filenames. this is because it frequently produces the same number - rand is only pseudo random. If you were to plot numbers returned on a graph you would get clusters.
    Here is a page on unique file names with code you can use.

    Your SQL code as written will produce a whole new property line in the database for each filename which I guess was not the intention! It needs to be outside the images while loop.

    To load the filenames just put:
    PHP Code:
    $image_filename=array(); 
    on the line before the while loop and
    PHP Code:
    $image_filename[]=$image_name
    immediately under where you store the filenames.


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
  •