Results 1 to 5 of 5

Thread: Uploading Image, Naming Problem

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Uploading Image, Naming Problem

    I have this upload page, and when it uploads, it just uploads the file, as a file, not a picture, with no extention. Here is my code:

    PHP Code:
    if($_POST["action"] == "Upload Image"){
        unset(
    $imagename);
        
    $memberid $_SESSION['member_id'];
        if(!isset(
    $_FILES) && isset($HTTP_POST_FILES))
            
    $_FILES $HTTP_POST_FILES;
        
        if(!isset(
    $_FILES['image_file']))
            
    $error["image_file"] = "An image was not found.";
        
        
    #One Directory for ALL pics, random filename
        
    $rand1 rand(199);
        
    $rand2 rand(100999);
        
    $rand3 rand(10009999);
        
    $rand $rand3 $rand2 $rand1;
        
    $imagename $memberid "_" $rand;
        
        
    /*
        #One dir for each member, original file names, or random is file name doesnt exitst
        $imagename = basename($_FILES['image_file']['name']);
        if(empty($imagename))
            $rand1 = rand(1, 99);
            $rand2 = rand(100, 999);
            $rand3 = rand(1000, 9999);
            $rand = $rand3 - $rand2 + $rand1;
            $imagename = $memberid . "_" . $rand;
        */
        
        
    if(empty($error))
        {
            
    $newimage "pics/" $imagename;
            
    //$newimage = "pics/$memberid/" . $imagename;
            //echo $newimage;
            
    $result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
            if(empty(
    $result)) {
                
    $error["result"] = "There was an error moving the uploaded file.";
            } else {
                
    $url "http://pics.rapidfriends.com/" $imagename;
                
    $sql1 "SELECT `picture_id` FROM `pictures` WHERE member_id='$memberid'";
                
    $query1 mysql_query($sql1);
                
    $num mysql_num_rows($query1);
                
                if (
    $num == "0") {
                    
    $sql2 "UPDATE `members` SET `default_pic_url`='$url' WHERE member_id='$memberid'";
                    
    $query2 mysql_query($sql2);
                }
                
    $caption $_POST['caption'];
                
    $sql "INSERT INTO `pictures` SET url='$url', member_id='$memberid', caption='$caption'";
                
    $query mysql_query($sql);
                echo 
    mysql_error();
                echo 
    "Your Picture Was successfuly Uploaded:<br><br>";
                include(
    "includes/functions.php");
                
    $showimg getimagesize($url); 
                
    $imgtag imageResize($showimg[0], $showimg[1], 150);
                echo 
    "<img src='$url' title='$caption$imgtag><br><br>Direct URL: $url<br><br><br>";
            }
        }


    in the db, the url is as follows: http://pics.rapidfriends.com/13_6510
    notice no extention, and it still goes to a picture, with the jpeg format... I dont understand whats going wrong...
    My usual boring signature: Something

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Uploading Image, Naming Problem

    uhh, maybe because you're making the image file's name like this?

    PHP Code:
    $imagename $memberid "_" $rand
    //............
    $newimage "pics/" $imagename
    if YOU don't add the extension, it won't be there. you can use this:
    PHP Code:
    $file $_FILES['image_file']['tmp_name'];
    $pos strrpos($file'.');
    $extension substr($file$posstrlen($file) - $pos);

    $imagename .= "{$memberid}_{$rand}{$extension}"

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Uploading Image, Naming Problem

    why are there brackets in the string?
    My usual boring signature: Something

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Uploading Image, Naming Problem

    curly brackets within a string denote using a variable. this avoids having to break your string or escape something when you don't need to.

    PHP Code:
    <?php
      
    //example 1:
      
    echo "$this['array']['wont']['print']";
      echo 
    "{$this['array']['will']}";
      
      
    //example 2:
      
    $var1 'string';
      
    $var2 'text';
      
    //the following will not print as expected:
      
    echo "$var1_$var2";
      
    //PHP will be looking for a variable named either "$var1_" and "$var2", or just one variable named "$var1_text". ("text" comes from adding $var2's actual value onto the variable reference because there is no space)
      //the following will print as expected:
      
    echo "{$var1}_{$var2}";
    ?>
    hope that made sense @_@

  5. #5

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Uploading Image, Naming Problem

    oh ok. That makes sense thanks!
    My usual boring signature: Something

Posting Permissions

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



Click Here to Expand Forum to Full Width