Configure file uploads with PHP

更新时间:
复制 MD 格式

This topic describes how to use a PHP upload program to add a file upload feature to your website.

Prerequisites

You must have the FileZilla client installed. You can download it from the official FileZilla website.

Note

This topic uses FileZilla 3.59.1 as an example. The user interface may vary depending on your version of FileZilla.

Background information

This topic provides a sample PHP upload program to enable a file upload feature. The example includes two files: php-upload.htm and php-upload.php. You can adapt this code to create your own program and customize the feature's style.

Limitations

  • This topic applies only to Alibaba Cloud Web Hosting instances running a Linux operating system.

  • In this example, the maximum upload file size is 50 KB.

  • Test filenames cannot contain Chinese characters. We recommend using English characters for the filename.

Procedure

  1. Open a code editor, such as EditPlus.

    Note

    You can also use another code editor of your choice.

  2. Copy the following sample code and save it as php-upload.htm and php-upload.php.

    Important

    When saving the files, set the encoding format to ANSI to prevent garbled characters.

    The following code is for the php-upload.htm file:

    <html>
    <body>
    
    <form action="php-upload.php" method="post"
    enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="Upload" />
    </form>
    </body>
    </html>

    The following code is an example of the php-upload.php file:

    <?php
    if ($_FILES["file"]["size"] < 50000)
      {
      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($_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          $_FILES["file"]["name"]);
          echo "Stored in: " . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
  3. Use FileZilla to connect to your Alibaba Cloud Web Hosting instance.

    For more information, see Manage website files by using FileZilla.

  4. Create a directory, such as test, to store the php-upload.htm and php-upload.php files.

    1. In the /htdocs site root directory of your Alibaba Cloud Web Hosting instance, right-click a blank area and click Create directory to create a new directory, such as test.

    2. In the Create directory dialog box, enter test for the directory name, and click OK.

  5. Set the read, write, and execute permissions for the new directory.

    1. Right-click the test directory and select File permissions....

    2. In the Change file attributes dialog box, select all read, write, and execute permissions, and then click OK.

      The file permissions are set as follows:

      Select Read, Write, and Execute for owner permissions, group permissions, and public permissions. The corresponding numeric value is 777.

  6. On your local computer, select the php-upload.htm and php-upload.php files. Then, use FileZilla to upload them to the test directory.

  7. In your browser, access http://your-domain-name/test/php-upload.htm.

    The file upload form appears on the page, containing a Filename: label, a Choose File button, and an Upload button.

  8. Click Choose File and select the test file to upload.

    Note

    This step uses the test file testfile.txt as an example. You can also upload other types of files for testing.

  9. Click Upload.

    In this example, after the file is successfully uploaded, the page displays information such as the filename, file type, and file size. You can find the uploaded testfile.txt in the /htdocs/test directory. The page also shows the storage path of the temporary file.

    Note

    During the upload, PHP creates a temporary copy of the file in its temporary folder. This copy is removed after the script finishes running, and the uploaded file is saved to the /htdocs/test directory.