Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Location
    Trowbridge, Wiltshire
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with image upload needed

    Hi,

    I am trying to create an image upload that changes the name of the image i am uploading, from "example.jpg" to "photo1.jpg" so when i upload a new photo to the database it adds the id number onto the end of the name, so when i upload image 306, the name will be "photo306", hope this makes sence.

    //database table//

    album_id | p_id | p_name
    - - - - - - - - - - - - - - -
    _ _1 _ _ _ _ 1 _ _ image1.jpg
    _ _1 _ _ _ _ 2 _ _ image2.jpg

    - - - - - - - - - - - - - - -

    p_id "stands for photo_id" = index key
    p_name "stands for photo_name" = name of file

    //end database//

    I have created some code that uploads the image to the directory and places the name of the image into the database but im not sure how to change the name to "photo1" "photo2" and so on, instead of the original image name. Im not even sure if this can be done? I am a bit of a beginner to php and mysql so it all seems a bit tricky! lol

    heres my code:


    PHP Code:
    <?php
    $host
    ="localhost"
    $username="root"
    $password=""
    $db_name="mydb"
    $tbl_name="photos"
    $id=$_GET['uid'];
    $album_id=$_POST['txtaid'];

    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    ?>

    <?php
    if (isset($_POST['submit'])) { 
        if (
    is_uploaded_file($_FILES['image']['tmp_name'])){
        
    $iname $_POST['id'] . "_" $_FILES['image']['name'];
        
    $istring "../../photos/thumbs/" $iname;
        if (
    move_uploaded_file($_FILES['image']['tmp_name'], $istring)){
            echo
    'The File Has Been Uploaded';
                
            
    $query ="INSERT into $tbl_name(album_id, p_name)VALUES('$album_id', '$iname')";
                
            if (
    $result =@mysql_query ($query)) {
            echo 
    '<br>Successfull';

            }else{
            echo 
    'not added';
            echo 
    'back to try again';
            }
            }else{
            echo
    'sorry not worked';

            
    $i="";
            }
            
        }else{
        
    $i="";
        }    
    }else{
    ?>

    hope somebody can help, thanks for looking

    cheers

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    After you insert the record into the database. You get the ID of it this way:

    PHP Code:
    $id mysql_insert_id(); 
    Then use rename() to rename the file:

    PHP Code:
    rename($oldfile'photo' $id '.jpg'); 
    Then of course don't forget to update the database with the new filename.

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Location
    Trowbridge, Wiltshire
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi thanks for your reply.

    I entered the code in as you said but i got a couple of errors.


    i had an // Notice: Undefined variable: oldfile
    and // Warning: rename(,photo0.jpg): Permission denied

    I think i have probably entered the code in wrong but am not sure

    PHP Code:
    <?php
    if (isset($_POST['submit'])) { 
        if (
    is_uploaded_file($_FILES['image']['tmp_name'])){
            
    $iname $_POST['id'] . "_" $_FILES['image']['name'];
            
    $istring "../../photos/thumbs/" $iname;
            if (
    move_uploaded_file($_FILES['image']['tmp_name'], $istring)){//move the file over.

                
    echo'The File Has Been Uploaded';
                

                
    $query ="INSERT into $tbl_name(album_id, p_name)VALUES('$album_id', '$iname')";
                
                
    $id mysql_insert_id();
                
    rename($oldfile'photo' $id '.jpg');
                
    $query ="UPDATE $tbl_name SET p_name='' where p_id='$id'";
                

                
                if (
    $result =@mysql_query ($query)) {//worked
                    
    echo '<br>Successfull';

    sorry for this but im a beginner and its taken me a while to get used to it and seeing what im missing.

    cheers for help!!!


  •  

    Posting Permissions

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