SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Midlands, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems uploading images and renaming..

    ok guys hope you can help again,

    i have an image upload part of a script which renames part of the file

    here is code:

    PHP Code:
                 <? 
                 $result 
    mysql_fetch_object(mysql_query("select id from car ORDER BY id DESC LIMIT 1"));
                 
    $ID $result->id 1;
                 
    ?>

    <? if($_POST['submit']){ 
                 
    $uploaddir '../images/cars/'
                 for(
    $i=0$icount($_FILES['userfile']); $i++){ 
                 
    $error_header "<b>The following errors occurred, please rectify them before continuing</b><br>"
                 
    $max_filesize 1024000;  //  you can edit this 
                 
    $img_types = array('gif''jpeg''jpg''JPG''JPEG''png'''); 
                 
    $ext substr(stristr($_FILES['userfile']['name'][$i],'.'),1); 
                 if (!
    in_array($ext$img_types)) { 
                 
    $error_msg =  "That file type is not allowed. <br>"
                 } 
                 if (
    $_FILES['userfile']['size'][$i] > $max_filesize) { 
                 
    $error_msg .=  "That file is too large!"
                 } 
                 
    $tempname $_FILES['userfile']['tmp_name'][$i]; 
                 
    $filename $_FILES['userfile']['name'][$i]; 
                 if(
    $tempname != ''){ 
                 
    move_uploaded_file($tempname$uploaddir."$ID".$filename.""); 
                 }   
            } 
     } 
    ?>
    The files have to be uploaded with the names, one.jpg, two.jpg etc else all the calls in the script wont work,

    any way of modding this code to rename the whole files to "<? echo $ID ?>one.jpg" even if the uploaded file name was for example "clio.jpg"

    will i also have to change the db and add tables in there??

    whats best way to go about it..
    Last edited by sjacko; Nov 24, 2004 at 16:49.

  2. #2
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if i get this right... but maybe something like this will answer your question? (seems to easy to me)
    PHP Code:
    ...
      
    $tempname $_FILES['userfile']['tmp_name'][$i];  
     
    $filename "$ione.$ext";  
     if(
    $tempname != ''){  
        
    move_uploaded_file($tempname$uploaddir."$ID".$filename."");  
      }
      ... 
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Midlands, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this wont work with multiple images though will it?

  4. #4
    SitePoint Zealot lemkepf's Avatar
    Join Date
    Feb 2002
    Location
    WI, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woops... read it wrong. Let's try this I see that you have an "id" variable at top.... let's try this again:
    PHP Code:
    ...
        
    $tempname $_FILES['userfile']['tmp_name'][$i];  
         
    $filename "$ID$i.$ext";  //creates a file called id3.ext
         
    if($tempname != ''){  
          
    move_uploaded_file($tempname$uploaddir.$filename."");  
        }
     ... 
    if you need the "one" "two" "three" thing instead of just a number (which is way easier to deal with....) You'll have to use this function i stumbled across a while back on the php.net comments:
    PHP Code:
    <?
         
    /**
         * convert long integer into American English words.
         * e.g. -12345 -> "minus twelve thousand forty-five"
         * Handles negative and positive integers
         * on range -Long.MAX_VALUE .. Long.MAX_VALUE;
         * It cannot handle Long.MIN_VALUE;
         */
         
         
    function num2words$num ){
             
    $ZERO "zero";
             
    $MINUS "minus";
             
    $lowName = array(
                   
    /* zero is shown as "" since it is never used in combined forms */
                   /* 0 .. 19 */
                   
    """one""two""three""four""five",
                   
    "six""seven""eight""nine""ten",
                   
    "eleven""twelve""thirteen""fourteen""fifteen",
                   
    "sixteen""seventeen""eighteen""nineteen");
         
             
    $tys = array(
                   
    /* 0, 10, 20, 30 ... 90 */
                   
    """""twenty""thirty""forty""fifty",
                   
    "sixty""seventy""eighty""ninety");
         
             
    $groupName = array(
                   
    /* We only need up to a quintillion, since a long is about 9 * 10 ^ 18 */
              /* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
                   
    """hundred""thousand""million""billion",
                   
    "trillion""quadrillion""quintillion");
         
             
    $divisor = array(
              
    /* How many of this group is needed to form one of the succeeding group. */
              /* American: unit, hundred, thousand, million, billion, trillion, quadrillion, quintillion */
                   
    10010100010001000100010001000) ;
         
             
    $num str_replace(",","",$num);
             
    $num number_format($num,2,'.','');
             
    $cents substr($num,strlen($num)-2,strlen($num)-1);
             
    $num = (int)$num;
         
             
    $s "";
         
             if ( 
    $num == $s $ZERO;
             
    $negative = ($num );
             if ( 
    $negative $num = -$num;
         
             
    // Work least significant digit to most, right to left.
             // until high order part is all 0s.
             
    for ( $i=0$num>0$i++ ) {
                 
    $remdr = (int)($num $divisor[$i]);
                 
    $num $num $divisor[$i];
                 
    // check for 1100 .. 1999, 2100..2999, ... 5200..5999
                 // but not 1000..1099,  2000..2099, ...
                 // Special case written as fifty-nine hundred.
                 // e.g. thousands digit is 1..5 and hundreds digit is 1..9
                 // Only when no further higher order.
                 
    if ( $i == /* doing hundreds */ && <= $num && $num <= ){
                     if ( 
    $remdr ){
                         
    $remdr += $num 10;
                         
    $num 0;
                     } 
    // end if
                 
    // end if
                 
    if ( $remdr == ){
                     continue;
                 }
                 
    $t "";
                 if ( 
    $remdr 20 ){
                     
    $t $lowName[$remdr];
                 }
                 else if ( 
    $remdr 100 ){
                     
    $units = (int)$remdr 10;
                     
    $tens = (int)$remdr 10;
                     
    $t $tys [$tens];
                     if ( 
    $units != ){
                        
    $t .= "-" $lowName[$units];
                     }
                 }else {
                     
    $t $inWords($remdr);
                 }
                 
    $s $t " " $groupName[$i] . " "  $s;
                 
    $num = (int)$num;
             } 
    // end for
             
    $s trim($s);
             if ( 
    $negative ){
                 
    $s $MINUS " " $s;
             }
         
             
    $s .= " and $cents/100";
         
             return 
    $s;
         } 
    // end inWords
         
         
    ?>
    So you would use this by doing something like:
    PHP Code:
         ...
        
    $tempname $_FILES['userfile']['tmp_name'][$i];  
         
    $filename "$ID"num2words($i) . ".$ext";  //creates a file called idthree.ext
         
    if($tempname != ''){  
          
    move_uploaded_file($tempname$uploaddir.$filename."");  
        }
        ... 
    Happiness is a belt-fed weapon
    Consciousness: That annoying time between naps.
    Modevia Web Services LLC.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Midlands, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it works fine but the ID isnt right.

    i tested it with 5 images and it renamed them to the following:

    250.jpg
    251.jpg
    252.jpg
    253.jpg
    254.jpg

    instead of the 0,1,2,3,4 i need it to say 25one, 25two, 25three.

    my original script did this but the uploaded files had to be named by me as one.jpg, two.jpg etc before i uploaded them.

    if you understand..

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Midlands, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually thinking about it, it will be fine like this, lemkepf cheers for your help on this dude.

    appreciate it.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Midlands, UK
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another quick tiny question which i totally forgot about, i only want it to upload .jpg and give an error if its any other extension.

    or cause in the main script i call the <img scr="<? echo $ID ?>0.jpg"> if someone uploads a .gif file it wont show up on main page, so either i limit it to .jpg or i edit the main page on frontend to search either its a .gif or .jpg


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
  •