SitePoint Sponsor

User Tag List

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

    Question need a little help with this upload code

    at the moment peeps who upload a photo gets a time stamp in the name
    so the final photo gets time stamp + the original name
    can this code be changed so as the photo only has the time stamp as name
    cheers
    Doug

    PHP Code:
    $pnametime().'_'.basename$_FILES['photo']['name']);

    //Writes the photo to the server 
     
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target.$pname))

    //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"
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  2. #2
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $pname time() . end(explode('.'$_FILES['photo']['name'])); 

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for reply
    can you explain simply what this does and how
    cheers
    Doug
    havnt tried it yet
    Last edited by dougvcd; Jul 23, 2009 at 01:33. Reason: more info
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, the time() function returns the timestamp.

    Explode('.', $_FILES['photo']['name']) returns an array which consists of parts of the filename split by a '.', and the end() function returns the last item.

    So, say the filename is 'hello.txt'. The explode return an array with two items - the first is 'hello' and the second is 'txt'.

    So the last item is the file extention.

    SO what the whole line will return with the 'hello.txt' file, with the timestamp of 1248345880, would return 1248345880.txt
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok changed the code to above and it is still not working as i wanted
    ok the photo i uploaded was dog.jpg
    the time stamp was added so the photo was named
    1248364178_dog.jpg and this was entered in database
    i was expecting 1248364178.jpg or is this not possable
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops.. there was a missing dot from the filename. Did not test it at the time I wrote it. This is tested code and does exactly what you wanted.

    PHP Code:
    $pname time() . '.' end(explode('.'$_FILES['photo']['name'])); 

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Post your code as you have it with TeNDoLLA's addition please Doug
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok getting totally confused now state of play is now timestamp is working ok
    it renames the photo to timestamp.jpg also puts that in database but does not display on web page
    here is the full script i have

    PHP Code:
    //This is the directory where images will be saved 
    $target "uploads/"

    //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 time() . '.' end(explode('.'$_FILES['photo']['name']));
    // Connects to your Database 
     
    $dbh=mysql_connect("localhost""cavcd""57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("pets"); 

    //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.$pname))


    //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 
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  9. #9
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have not told the script to show it anywhere. To show an image on page you need to use html's <img src="/path/tto/your/image.jpg"> tag.

  10. #10
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    but that happens on another web page
    can a time stamp on its own be used as a name or is it my server not doing it
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  11. #11
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does this iamge showing happen on this another page, if the images are not shown ? Yes if you give image name a timestamp then the image is named timestamp.jpg and then you must use this absolutely same name/path to show the image later. Not with new timestamp. I am not really following you... what you try to say?

  12. #12
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry if i am confusing you
    what i ment to say when the photo is up loaded its name is changed to the time stamp or am i wrong
    so that in the upload folder the photo would be say 125342653.jpg
    and in the database under pname would be the same 125342653.jpg
    if you see what i mean
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  13. #13
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So after the upload and db insertion, you have this recently uploaded images name stored in some variable, then you do somewhere where you want:
    PHP Code:
    echo '<img src="'$target $pname .'" />'
    Or if you want to fetch the images from db and show many images.
    PHP Code:
    $sql "SELECT pname FROM images LIMIT 0, 20";
    $result mysql_query($sql);
    while (
    $row mysql_fetch_object($result))
    {
       echo 
    '<img src="'$target $row->pname .'" /><br/>';

    this any help?

  14. #14
    SitePoint Addict
    Join Date
    Jun 2007
    Location
    Plymouth uk
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is how i display

    PHP Code:
    //Outputs the image and other data
    Echo "<img src=http://www.lostpetsplymouth.com/uploads/".$info['pname'] ." alt=\"Image\" align=\"left\" width=\"75px\" height=\"75px\" hspace=\"10px\" vspace=\"8px\"> <br>";
    Echo 
    "<b>Description:</b> ".$info['desc'] . " <br>";
    Echo 
    "<b>Area:</b> ".$info['area'] . " <br>";
    Echo 
    "<b>Tel:</b> ".$info['tel'] . " <br>";
    Echo 
    "<b>Date of listing:</b> ".$info['date'] . " <br/>";

    Echo 
    "<hr>";
    }
    Echo 
    '<font color="red"> **** END OF RECORDS ****</font>';

    ?> 
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  15. #15
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And what happens? Does it give any error? Broken image? Absolutely nothing? Have you checked what is inside $info['pname'], is there data that should be there (try with echo, var_dump or print_r)? See also that this image really exists in the hard-drive. Are you absolutely sure the url is correct?

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

    Red face

    i give you permision to hit me with a big stick
    your code is working right
    it is me i have been uploading wrong photo
    a big thank you all is working as it should do
    cheers
    Doug
    an old man of 60 trying to keep up with the youngsters he he
    http://lostpetsplymouth.net16.net

  17. #17
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets leave the big sticks for now.. I am not really into that kind of things..


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
  •