Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Help with...

  1. #1
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with...

    Alright im having trouble with something...
    What im looking for is something so if somone uploads an image and another person uploads another image with the same name say 'image.gif' id want the other image to be renamed 'image1.gif' how would i do this?? i found this


    PHP Code:
    <?php 
      $img 
    "images/".$_FILES['bilde']['name'];
      
    $t=0;
      while(
    file_exists($img)){
        
    $img "images/".$_FILES['bilde']['name'];
        
    $img=substr($img,0,strpos($img,"."))."_$t".strstr($img,".");
        
    $t++;
      }
      
    move_uploaded_file($_FILES['bilde']['tmp_name'], $img);
    ?>

    I'm having trouble with it... would somone be willing to help me out?

    Im useing it for a dynamic signature script i've been working on and finally got it working today. but it over writes the image name when i put in the same name, and if i let my visitors use it and some how they use the same name i wouldnt want it to mess up there signature with somone elses.

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    What trouble are you having?

  3. #3
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Haven't tested it but I think your problem is how you're treating $t.

    try this


    PHP Code:
      $img "images/".$_FILES['bilde']['name']; 
      
    $t=0
      while(
    file_exists($img)){ 
        
    $img "images/".$_FILES['bilde']['name']; 
        
    $img=substr($img,0,strpos($img,".")).$t.".".strstr($img,"."); 
        
    $t++; 
      } 
      
    move_uploaded_file($_FILES['bilde']['tmp_name'], $img); 

  4. #4
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    i know i sound kinda dumb right now but im like confused right now lol

    would 'bilde' be the file name? how would i make it so my file would work in here..

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    bilde is the name of the file input in your form:

    <input name="bilde" type="file" />

    $_FILES['bilde']['name'] is the name of the file as it is on the user's computer.
    $_FILES['bilde']['tmp_name'] is the name(path) of the file on the server running PHP (probably your web server).

  6. #6
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Oh alright.. sorry lol just now learning alittle bit of PHP..

    so ive got a form that looks like the one below on my site right now.

    Code:
    <form action="" method=POST>
    
    <input type="text" name="id" value="img">
    
    <input type="submit" value="Submit">
    </form>

    What it does once its been submitted is grabs the 'imagecreatefrompng' ive got and makes it into 'img.png' and then sends it to the directory 'images' how would i make it work for something like this?
    Last edited by Jeffreyv1987; 09-04-2009 at 08:43 PM.

  7. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    http://www.tizag.com/

    Go through some tutorials first. HTML and PHP.

    Good luck.

  8. #8
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Alright cool..

    Right now i just put a thing to check if the image exists and it tells the users that theres a image with the same name.

    but not sure what to do to make it the way i want..

  9. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Well, you should first be aware that if a file with the same name exists, the old one would be overwritten. Now when you say "image exists", do you mean that they inputted an image?

    THis so far will check if image exists, and if user inputted image:
    PHP Code:
    <?php

      $img 
    "images/".$_FILES['bilde']['name']; 
      
    $t=0
      while(
    file_exists($img)){ 
        
    $img "images/".$_FILES['bilde']['name']; 
        
    $img=substr($img,0,strpos($img,".")).$t.".".strstr($img,"."); 
        
    $t++; 
      } 

    if(isset(
    $_POST['blide'])) { if user inputted an image
      
    if(file_exists($_FILES['blide']['name'])) { // if file exists already
        
    echo "This file already exists in this directory.";
      } else { 
    // all req. met, upload file
        
    move_uploaded_file($_FILES['bilde']['tmp_name'], $img); 
        echo 
    "File successfully uploaded."
      }
    } else { 
    // if user did NOT input an image
    echo "You must input an image first!";
    }

    ?>
    and your form should look like:
    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    File: <input type="file" name="blide" />
    <input type="submit" value=" Upload " />
    </form>
    && like Jason said, you should probably go thru PHP and HTML tutorials first.

    HTH
    - Josh

  10. #10
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Alright ive got it working the way i want it thanks for helping people.

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
  •