How to support FTP and HTTP uploads via PHP

Source: Internet
Author: User
Tags http file upload ftp client ftp protocol
How to support FTP and HTTP uploads via PHP
In the process of learning programming, I think not only to obtain the knowledge of textbooks, more is to solve the problem of the method, the face of new problems how to solve, so that we can walk in the forefront, I am dahne students, this article reproduced in (http://www.phptarena.com) Thank you for your support;
With PHP, you can always have a number of ways to accomplish a particular task. Let's take a file upload for example. Of course, you can use HTTP file uploads in the traditional way, transferring files directly to the Web server disk. You can also upload in a more exotic way, upload it in two steps with the FTP protocol: from your local hard drive to the Web server, and then to the FTP server.

PHP supports both FTP and HTTP uploads natively, so you can make the best choice based on your application's design needs. Using PHP's FTP function for file transfer is almost identical to using a traditional FTP client-you'll see that the name of the hyphen is similar to the standard FTP command.

There are many more articles about HTTP file uploads, which is why this article needs to focus on FTP-based file uploads (but in the examples given later, you will see them). Note that this tutorial assumes that you have installed the Php/apache, and that the HTTP file upload and FTP functions have been activated.

The first step: Make sure you have permission to connect/upload to the FTP server

PHP's FTP function requires a client-server connection, so you need to log on to the target server before uploading the file. Your first task is to make sure that you already have a letter of trust to complete the task. This step may seem natural, but you'll be amazed at how many developers forget to do so, and then waste a lot of time solving the problems that arise.

You can log on to the target server by using the command line FTP client to check the connection status and attempt to upload a fake file

Once you have identified the relevant access rights, you can log in.

Step Two: Create an upload form

Then, create a simple HTML form to ask the user important parameters--ftp the server's access information, the server's uploaded directory, and the full directory and the name of the uploaded file. The following example is the style of this form.

Over here The element is used to generate a file selection dialog where the user selects the file to be uploaded. element then encodes the form data into multiple partial commits, which makes it easier for PHP to identify the submitted file component.

Step three: Create a PHP upload handler

Once the form is submitted to the Web server, the next (and final) step is to use PHP's FTP function to transfer it to the target server in accordance with the user-supplied access trust book. Here is the script (upload.php) that completes all of the above work.
Knowledge is power, others learn to use knowledge to make money, the cost is low, in Dahne learning to program is casting dream Place, if you like programming, here, have our common language, common story, Common dream!
(Editor: Guangzhou Dahne)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.