PHP file upload form

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

    #1

    PHP file upload form

    Hello
    I am trying to create a simple file upload form so my band members can
    upload mp3s onto my server. I am using this tutorial:

    Learn the basics of uploading files with PHP by reading Tizag.com's PHP File Upload lesson.



    I am having problems creating the code for uploader.php. Can anyone
    show me exacly how it should look?

    Thanks

  • Jacob Lyles

    #2
    Re: PHP file upload form

    I did this for the first time the other day.

    On the HTML side:

    <form enctype="multip art/form-data" action="/upload.php" method="POST"[color=blue]
    >[/color]
    Send this file: <input name="userfile" type="file" /> <br />
    <input type="submit" value="Send File" />
    </form>


    And your PHP action:

    $uploaddir = './dir/';
    $file_name = $_FILES['userfile']['name'];
    $uploadfile = $uploaddir . $file_name;
    move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);


    If you're buying third party hosting, sometimes they have a maximum
    file size which you'll want to watch out for.

    Comment

    • bjkstudio@hotmail.com

      #3
      Re: PHP file upload form

      Is this how my uploader.php file should look? Does the function spcript
      go directly beneath the
      php code? Thanks
      -----------------------------------



      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html;
      charset=ISO-8859-1">
      <title>Untitl ed Document</title>
      </head>
      // Where the file is going to be placed
      $target_path = "uploads/";

      /* Add the original filename to our target path.
      Result is "uploads/filename.extens ion" */
      $target_path = $target_path . basename(
      $_FILES['uploadedfile']['name']);
      $_FILES['uploadedfile']['tmp_name'];

      $uploaddir = './dir/';
      $file_name = $_FILES['userfile']['name'];
      $uploadfile = $uploaddir . $file_name;
      move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);
      <body>
      </body>
      </html>

      Comment

      • Jacob Lyles

        #4
        Re: PHP file upload form

        I think this is what you want:

        <?php
        $target_path = "./uploads/";
        $target_path = $target_path .
        basename($_FILE S['uploadedfile']['name']);
        move_uploaded_f ile($_FILES['uploadedfile']['tmp_name'], $target_path);
        ?>


        It's a php page, so you don't want any html output. I'm assuming the
        name of the files section of your form is "uploadedfi le". Also, the
        <form> tag should have the attribute
        action="whateve r_you_called_th is_file.php". You really should learn how
        these commands work using the PHP manual. Check out:






        Both links offer great instruction and some tutorials.

        Comment

        • Jacob Lyles

          #5
          Re: PHP file upload form

          Oh, and on my first example I forgot to close my first <form> tag with
          the right bracket. Don't forget to close yours.


          bjkstudio@hotma il.com wrote:[color=blue]
          > Is this how my uploader.php file should look? Does the function spcript
          > go directly beneath the
          > php code? Thanks
          > -----------------------------------
          >
          >
          >
          > <html>
          > <head>
          > <meta http-equiv="Content-Type" content="text/html;
          > charset=ISO-8859-1">
          > <title>Untitl ed Document</title>
          > </head>
          > // Where the file is going to be placed
          > $target_path = "uploads/";
          >
          > /* Add the original filename to our target path.
          > Result is "uploads/filename.extens ion" */
          > $target_path = $target_path . basename(
          > $_FILES['uploadedfile']['name']);
          > $_FILES['uploadedfile']['tmp_name'];
          >
          > $uploaddir = './dir/';
          > $file_name = $_FILES['userfile']['name'];
          > $uploadfile = $uploaddir . $file_name;
          > move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);
          > <body>
          > </body>
          > </html>[/color]

          Comment

          Working...