PHP upload - rename uploaded file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smilesinblues@hotpop.com

    #1

    PHP upload - rename uploaded file

    Hi,
    I have to allow my visitors to upload image on my site.

    I am using the follwoing code to do that:

    $uploaddir = 'admin/';
    $uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);

    My problem is that I expect them to use same file name, like all of
    them will be uploading files like, comments.rtf. So if there is already
    one comments.rtf the file will get overwriten... so the challenge is to
    rename the files to username-filename.rtf

    can anybody here will take the pain to modify this code, I will be
    thankful, or please guide to me some tutorial where they are dealing
    with simple code and not with advanced classes.
    Thank you in advane

    Regards,
    Jaunty Edward

  • John Wellesz

    #2
    Re: PHP upload - rename uploaded file

    On 14 déc. 2004, Sir smilesinblues@h otpop.com claimed in
    news:1103007578 .311827.196640@ f14g2000cwb.goo glegroups.com:
    [color=blue]
    > Hi,
    > I have to allow my visitors to upload image on my site.
    >
    > I am using the follwoing code to do that:
    >
    > $uploaddir = 'admin/';
    > $uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    > move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);
    >
    > My problem is that I expect them to use same file name, like all of
    > them will be uploading files like, comments.rtf. So if there is already
    > one comments.rtf the file will get overwriten... so the challenge is to
    > rename the files to username-filename.rtf
    >[/color]


    In your code there is a line where you are storing the real name of the
    file in $uploadfile (that you should have named $uploadedfile), so the
    action you have to take is on that line...

    You should learn to program in PHP, this is a very easy language, and the
    better way to learn is to read the PHP manual from the very beginning at
    http://www.php.net.

    Good luck.

    Comment

    • smilesinblues@hotpop.com

      #3
      Re: PHP upload - rename uploaded file

      Hi,
      I fixed it with someone's help, now next problem is I am using
      ($_FILES['userfile']['type'] == "image/gif") to check if the file is
      gif, its working but if I change the mime type to jpeg or png or
      anything else its not working.

      i took the mime types from http://www.sfsu.edu/training/mimetype.htm

      so if I use $_FILES['userfile']['type'] == "image/png" it does'nt work
      not with jpeg and jpg also I don't know what to do.

      Please help

      Comment

      • Daniel Tryba

        #4
        Re: PHP upload - rename uploaded file

        smilesinblues@h otpop.com wrote:[color=blue]
        > I fixed it with someone's help, now next problem is I am using
        > ($_FILES['userfile']['type'] == "image/gif") to check if the file is
        > gif, its working but if I change the mime type to jpeg or png or
        > anything else its not working.
        >
        > i took the mime types from http://www.sfsu.edu/training/mimetype.htm
        >
        > so if I use $_FILES['userfile']['type'] == "image/png" it does'nt work
        > not with jpeg and jpg also I don't know what to do.[/color]

        This is really basic stuff you are asking here. You should lookup the
        logical operators in the manual:

        if($a || $b)
        {
        echo '$a and/or $b is true';
        }
        else
        {
        echo 'neither $a or $b were true';
        }

        Comment

        • smilesinblues@hotpop.com

          #5
          Re: PHP upload - rename uploaded file

          Oh,
          you are getting me wrong. I am not asking for if else loop or anything,
          what i am talking about is only one file type.

          if I use only gif it works, if I only use jpeg or png it does'nt.

          SO the question is not if else, I only want to use one mime type, I
          will handle the rest.

          Thanks.

          Comment

          • Norman Peelman

            #6
            Re: PHP upload - rename uploaded file

            <smilesinblues@ hotpop.com> wrote in message
            news:1103183447 .919733.230010@ z14g2000cwz.goo glegroups.com.. .[color=blue]
            > Oh,
            > you are getting me wrong. I am not asking for if else loop or anything,
            > what i am talking about is only one file type.
            >
            > if I use only gif it works, if I only use jpeg or png it does'nt.
            >
            > SO the question is not if else, I only want to use one mime type, I
            > will handle the rest.
            >
            > Thanks.
            >[/color]
            [color=blue]
            >Hi,
            >I fixed it with someone's help, now next problem is I am using
            >($_FILES['userfile']['type'] == "image/gif") to check if the file is
            >gif, its working but if I change the mime type to jpeg or png or
            >anything else its not working.[/color]

            1) What mime type (file type) doyou want to allow?
            2) How are you handling the file if it is not what you want?
            3) For testing purposes, try 'echoing' the $_FILES['userfile']['type'] to
            the screen to see what it is (and why it isn't working)

            Try:

            echo "\n\r <!-- INFO: mime type of uploaded file is:
            $_FILES[userfile][type] --> \n\r";

            This will print in your HTML output, not directly on the screen, so you will
            have to 'View Source' from your browser to find it.


            Norm
            --
            Avatar hosting at www.easyavatar.com


            Comment

            • smilesinblues@hotpop.com

              #7
              Re: PHP upload - rename uploaded file

              HI,
              thank you Norman, after looking at the echo I realised that the file
              type should be image/x-png and not image/png....

              Now I will find all extensions with the echo...and then use them in if
              else.

              Thank you all of you.

              Bye

              Comment

              • Norman Peelman

                #8
                Re: PHP upload - rename uploaded file

                <smilesinblues@ hotpop.com> wrote in message
                news:1103254388 .295523.171860@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                > HI,
                > thank you Norman, after looking at the echo I realised that the file
                > type should be image/x-png and not image/png....
                >
                > Now I will find all extensions with the echo...and then use them in if
                > else.
                >
                > Thank you all of you.
                >
                > Bye
                >[/color]

                That mime-type list is incorrect (a least as far as png goes).

                Norm
                --
                Avatar hosting at www.easyavatar.com


                Comment

                Working...