PHP Image Upload Question

Discussion in 'PHP' started by MajHate, Jun 14, 2007.

  1. #1
    Hi everyone,

    I have another question, im uploading an image to my ftp, and the name of it to MYSQL Database. I need to get the .jpg or .gif, how do I do that? Heres my code:

           $number = "1";
    
          $new_name = $car_name . "" .$number; 
    
    
    	$upload = move_uploaded_file($_FILES['pic1']['tmp_name'], $images_path . "/" . $new_name);
    
          // Upload the file 
           
          if ($upload) { 
          // If the file was 
             echo "works";
           }
    
    Code (markup):
     
    MajHate, Jun 14, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    $filename = "filename.jpg";
    
    echo end( split("\.", $filename ) );
    
    PHP:
     
    krakjoe, Jun 14, 2007 IP
  3. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I'm sorry, I am not very good at PHP, I don't understand this code.
     
    MajHate, Jun 14, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?php
    $number = "1";
    
          $new_name = $car_name . "" .$number; 
    
    
    	$upload = move_uploaded_file($_FILES['pic1']['tmp_name'], $images_path . "/" . $new_name);
    
          // Upload the file 
           
          if ($upload) { 
          // If the file was 
             printf('%s uploaded, it\'s extension is \'%s\'<br />', $images_path . "/" . $new_name, end( split('\.', $new_name ) ) );
           }
    ?>
    
    PHP:
     
    krakjoe, Jun 14, 2007 IP
  5. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    It still does not give me the .jpg, here is what it prints:

    ..../public_html/car_pics/pimpride1 uploaded, it's extension is 'pimpride1'


    the name of the file was pimpride1 but it didnt get pimpride1.jpg

    Do you know why?

    Thanks!
     
    MajHate, Jun 14, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    well I don't have all the information available because you didnt post the whole script, I assumed $new_name would contain the filename and extension, it seems it only contains the filename and number
     
    krakjoe, Jun 14, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
        $number = "1";
    
        $new_name = "$car_name$number." . end(explode('.', $_FILES['pic1']['name']); 
    
        $upload = move_uploaded_file($_FILES['pic1']['tmp_name'], $images_path . "/" . $new_name);
    
        // Upload the file 
           
        if ($upload) { 
            // If the file was 
            echo "works";
        }
    PHP:
     
    krt, Jun 14, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Don't use the tmp_name to get the file extension, because it'll be different that the original one. Use $_FILES['pic1']['name'].
     
    nico_swd, Jun 14, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    It's what happens when you are lazy and copy paste and then forget to change it...
    But you were not supposed to see that, I edited that a few seconds after I posted :p
     
    krt, Jun 14, 2007 IP
  10. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    move_uploaded_file($_FILES['pic1']['tmp_name'], $images_path . "/" . $new_name);
    PHP:
    Is this method from PHP?
     
    ProgrammersTalk, Jun 14, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #11
    function, and yeah it is, you can see its linked to the docs in the
     tags
    PHP:
     
    krakjoe, Jun 14, 2007 IP