SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question need some help with this upload script

    ok i use this code to add number to pic which uploads to server
    PHP Code:
    //This is the directory where images will be saved 
    $target "uploads/"
    $target $target .time().'_'.basename$_FILES['photo']['name']); 
    now i need to know how to add that name to table in database
    this is what i use at moment but it dosnt add the changed name

    PHP Code:
    //This gets all the other information from the form 
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $area=$_POST['area'];
    $desc=$_POST['desc'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $tel=$_POST['tel'];
    $date date("d-m-Y");
    $pname=($_FILES['photo']['name']); 
    //Writes the information to the database 
    mysql_query("INSERT INTO `register` VALUES ('$id', '$state', '$type', '$area', '$desc', '$name', '$email', '$tel', '$pname', '$date')");
    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 


    //Tells you if its all ok 
    echo "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"

    else { 

    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."
    }  
    //send the mail 
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  2. #2
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    replace $pname in your insert to $target.
    Make sure $target is defined before its used in the insert.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but i cant change the databse field to $target as pname is used else where
    thanks
    must look for another way
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eruna suggested that you change the value which you are inserting into the database, not the name of a table in the database. In your insert query, you are inserting the variable $pname, which is the original value of the photo name being posted in $_FILES['photo']['name']). You want this value to be replaced by the new name containing the timestamp, so simply change $pname to $target in your insert query and everything should work fine.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok changed that and this is what was entered into database
    uploads/1243280578_smudge.jpg
    didnt want the uploads part
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, take out this line
    PHP Code:
    $target $target .time().'_'.basename$_FILES['photo']['name']); 
    then, change
    PHP Code:
    $pname=($_FILES['photo']['name']); 
    to
    PHP Code:
    $pnametime().'_'.basename$_FILES['photo']['name']); 
    Then, in your insert query, revert to what you originally posted, i.e replace $target with $pname.

    And finally, change
    PHP Code:
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    to
    PHP Code:
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pname)) 
    let me know how you get on.

  7. #7
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    a big thank you that works fine
    i owe you a pint
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net


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
  •