Technology Programming

How to Upload Files in PHP Script

    • 1). Type the following into your file to create the user upload form:

      <form enctype="multipart/form-data" action="upload_photo.php" method="POST">

      Choose Image: <input name="userfile" type="file" /><br />

      <input type="submit" value="Upload Photo" />

      </form>

      Save the file, for example "upload_photo.html."

    • 2). Open a new file to create the PHP file needed to execute the "action" for the form. Type the following:

      <?php

      $ path = "profiles/photos";

      $path = $path . basename( $_FILES['userfile']['name']);

      if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) {

      echo "Successful upload of ". basename($_FILES['userfile']['name']);

      } else{

      echo "Error when uploading the file.";

      }

      ?>

      Save your file using "upload_photo.php" as the file name.

    • 3). Upload both the HTML and PHP files to your server. Test the form by navigating to your upload form Web page.



Leave a reply