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 9 of 9
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question File upload, if file exists then add a number?!

    Hi,

    I am doing a website with file upload facility and thought what if someone uploads a file with the same name as another?!

    maybe a number can be added after it ie;

    pic.jpg

    pic.jpg already exists so change to: pic1.jpg

    if pic1.jpg already exists the change to pic2.jpg and so on?!

    here is my code so far:

    Code:
    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("gallery/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "gallery/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "gallery/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,432
    Thanks
    62
    Thanked 536 Times in 523 Posts
    Ideally you should rename the file using uniqid() OR just modify it to contain iteg:

    picture.jpg becomes sdf987987-picture.jpg (or whatever uniqid() outputs.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    sorry, I'm not really following here?

    do I do this then?
    Code:
    uniqid($_FILES["file"]["name"]);
    what is iteg?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,432
    Thanks
    62
    Thanked 536 Times in 523 Posts
    PHP Code:
    $FileName uniquid() .'-' .$_FILES['file']['name']; 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    now I get the error:

    Fatal error: Call to undefined function uniquid() in D:\i-spie.com\wwwroot\upload_file.php on line 8

    here is my code:

    Code:
    <?php
    session_start();
    if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
    header ("Location: index.php");
    }
    mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
    mysql_select_db("xxx") or die(mysql_error());
    $FileName = uniquid() .'-' .$_FILES['file']['name'];  
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $FileName . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("gallery/" . $FileName))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "gallery/" . $FileName);
          
          // Define $myusername and $mypassword
    $UserID=$_POST['UserID'];
    $PictureName=$_POST['PictureName'];
    $PictureDate=$_POST['PictureDate'];
    $TargetID=$_POST['TargetID'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $UserID = stripslashes($UserID);
    $PictureName = stripslashes($PictureName);
    $PictureDate = stripslashes($PictureDate);
    $TargetID = stripslashes($TargetID);
    $UserID = mysql_real_escape_string($UserID);
    $PictureName = mysql_real_escape_string($PictureName);
    $PictureDate = mysql_real_escape_string($PictureDate);
    $TargetID = mysql_real_escape_string($TargetID);
    
          
          $sql = "INSERT INTO rstargetpictures (UserID, FileName, PictureName, PictureDate, TargetID)
    VALUES ('$UserID', '$FileName', '$PictureName', '$PictureDate', '$TargetID')";
    $msg = "New booking added";
    $result = mysql_query($sql) or die('Error: ' . mysql_error() . '<br>SQL: ' . $sql);
          
          echo "Stored in: " . "gallery/" . $FileName;
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Please please please try debugging for yourself, the PHP site is exceptionally helpful. tangoforce just typoed the function name is all:
    http://www.php.net/uniqid

  • #7
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    ah yeah, thanks

  • #8
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    try this

    PHP Code:
    $FileCounter 1;
    while (
    file_exists'gallery/'.$file_name ))
    $file_name $FileCounter++.'_'.$file_name

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,432
    Thanks
    62
    Thanked 536 Times in 523 Posts
    Quote Originally Posted by kbluhm View Post
    Please please please try debugging for yourself
    I second that. There is nothing better than beating the computer

    Seriously though jarv, for the last few months you don't seem to have learned anything despite all of our help. Are you actually trying to learn or just pick up snippets of code and mix them all together?

    PS sorry for the typo there but as kbluhm says a couple of seconds on php.net would of detected that along with looking at my previous ways of spelling it.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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