Home php教程 php手册 用PHP实现文件上传二法

用PHP实现文件上传二法

Jun 21, 2016 am 09:13 AM
ftp gt lt quot

上传

PHP(Hypertext Preprocessor)是一种 HTML 内嵌式的语言 (类似 IIS 上的ASP)。而 PHP 独特的语法混合了 C、Java、Perl 以及 PHP 式的新语法。它可以比 CGI 或者 Perl 更快速的执行动态网页。除此之外,用 PHP 写出来的 Web 后端CGI 程序,可以很轻易的移植到不同的系统平台上。

  我们在做网站时,需要访问者的参于才能将网站建设得更加引人注目,这就要求我们从访问者那里得到文章、图片等。因此,文件上传成为网页中必不可少的功能。现在我就利用现在流行的编程语言PHP,用两种方法来说明其功能的实现。

一、利用php的文件函数来实现上传
  这段代码分为两个文件,一个为upload.html,一个是upload.php。


上传文件选择:upload.html 代码如下:


--------------------------------------------------------------------------------

  

上载文件表单

  

  


    

  
请选择文件:

      

    

  


  


  

  


--------------------------------------------------------------------------------

*** 说明 ***

  1、请注意
这是一个标签,我们要实现文件的上传,必须指定为 multipart/form-data ,否则服务器将不知道你在干什么!

  2、值得注意的是文件upload.html中表单选项 MAX_FILE_SIZE 的隐藏值域,通过设置

其Value(值)可以限制上载文件的大小。


处理刚刚上传的文件:upload.php 代码如下:


--------------------------------------------------------------------------------

  

  

  处理上载文件

  

  

  

  copy($userfile,"newfile");

  echo $userfile."-用户上传到服务器上的文件临时存放的名称
";

  echo $userfile_name."-在用户机器上该文件的原始名称
";

  echo $userfile_size."-上传文件的实际字节数
";

  echo $userfile_type."-如果用户的浏览器提供了这个信息的话,它表示mime的类型。例如image/gif
";

  ?>

  

  


--------------------------------------------------------------------------------

*** 说明 ***

  1、使用了PHP文件函数copy()将上载到临时目录下的文件拷贝到一个特定的目录下,并重新命名为"newfile"。

  2、在upload.html中定义了一个变量 userfile,在upload.php中,我们就可以使用这个变量,直接通过$userfile访问上载的文件,但由于这是一个保存文件的变量,因此文件名字必须通过另外一个$userfile_name变量取得。

  下面是这个变量的具体用法:

  $userfile:在将要存放上载文件的服务器上的临时文件名字。

  $userfile_name:在发送者系统中的初始文件名。

  $userfile_size:按字节计算的上载文件的大小。

  $userfile_type:多用途网际邮件扩充协议类型的文件,前提是浏览器提供这种信息,比如说“image/gif"。



二、利用FTP功能进行文件上传
  这段代码同样分为两个文件,一个为upload.php,一个是ftp.php。


设置ftp的相关选项及选择上传文件名:upload.php 代码如下:


--------------------------------------------------------------------------------

  
  $username="用户名";

  $password="用户密码";

  $server="主机名";

  $cdir="上传目录名" ;

//以上设置你的FTP主机名、用户名和用户密码

  ?>



  




    >

    >

    >

    >

  

  

    

  

  

    

  

  
上传文件选择

    

    




    

    


  



--------------------------------------------------------------------------------


处理上传文件:ftp.php 代码如下:


--------------------------------------------------------------------------------

  
//ftp联接主机函数

  function connect()

  {

  global $server, $username, $password;

  $conn = ftp_connect($server);

  ftp_login($conn, $username, $password);

  return $conn;

  }

//建立ftp联接

  $result = connect();

  if ($action == "上传")

  {

//用来改变ftp路径

  ftp_chdir($result, $cdir);

//用来上传指定的文件,同名并以二进制位传递

  $res_code = ftp_put($result, $upfile_name, $upfile, FTP_BINARY);

// 判断是否正确上传

  if ($res_code == 1)

echo "上传成功!";

  else

echo "上传错误!";

  }

// 关闭联接

  ftp_quit($result);

  ?>


--------------------------------------------------------------------------------

*** 说明 ***

  函数ftp_put(int ftp_stream, string remote_file, string local_file, int mode)用法

  返回值: 布尔值

  本函数用来上传指定的文件。参数 ftp_stream 为 FTP 的连接代码。参数 remote_file 为欲存在远端的文件名。参数 local_file 为欲上传文件的文件名。参数 mode 的值有 FTP_ASCII 及 FTP_BINARY 二种,分别表示文档或者是二进位文件。成功则返回 true 值,失败则返回 false 值。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to set up nginx reverse proxy ftp server How to set up nginx reverse proxy ftp server May 17, 2023 am 09:31 AM

1. Install nginx 2. Install vsftpd 3. Modify the nginx configuration file nginx.conf 3.1 Add the ftp user userftpuser in the first line; 3.2 Configure the relevant path server{ listen80; #nginx proxy port server_namelocalhost; #ftp server address location/images{root /home/ftpuser; #The absolute path of the folder of the proxy ftp server indexftpuser; #Set the welcome page

Using FTP in Go: A Complete Guide Using FTP in Go: A Complete Guide Jun 17, 2023 pm 06:31 PM

With the rapid development of the Internet, File Transfer Protocol (FTP) has always been an important file transfer method. In Go language, using FTP to transfer files may be a need of many developers. However, maybe many people don't know how to use FTP in Go language. In this article, we will explore how to use FTP in Go language, from connecting to FTP server to file transfer, and how to handle errors and exceptions. Create FTP connection In Go language, we can use the standard "net" package to connect to FTP

PHP and FTP: realizing file sharing among multiple departments in website development PHP and FTP: realizing file sharing among multiple departments in website development Jul 28, 2023 pm 01:01 PM

PHP and FTP: Achieve file sharing among multiple departments in website development. With the development of the Internet, more and more companies are beginning to use website platforms for information release and business promotion. However, the problem that arises is how to achieve file sharing and collaboration among multiple departments. In this case, PHP and FTP become one of the most commonly used solutions. This article will introduce how to use PHP and FTP to achieve file sharing among multiple departments in website development. 1. Introduction to FTP FTP (FileTransferPr

What are the ftp commands under linux? What are the ftp commands under linux? Mar 21, 2023 am 09:59 AM

The ftp commands under Linux include: 1. ftp command; 2. close command; 3. disconnect command; 4. open command; 5. user command; 6. account command; 7. bye command; 8. quit command; 9. help command ;10. rhelp command; 11. ascii command; 12. binary/bi command; 13. bell command, etc.

How to compare directories and files on an FTP server via PHP How to compare directories and files on an FTP server via PHP Jul 28, 2023 pm 02:09 PM

How to compare directories and files on an FTP server through PHP In web development, sometimes we need to compare local files with files on the FTP server to ensure consistency between the two. PHP provides some functions and classes to implement this functionality. This article will introduce how to use PHP to compare directories and files on an FTP server, and provide relevant code examples. First, we need to connect to the FTP server. PHP provides the ftp_connect() function to establish an FTP server

How to add/delete FTP users and set permissions in Linux How to add/delete FTP users and set permissions in Linux May 12, 2023 pm 08:46 PM

1. Environment: ftp is vsftp. The username is set to test. The restricted path is /home/test2. Create a user: under the root user: useradd-d/home/testtest#Add user test, and set the home directory of the test user to /home/testpasswdtest#Set a password for the test user3. Change the corresponding permission settings of the user: 1.usermod-s/sbin/nologintest#Limit user test cannot telnet, only ftp2.usermod-s/bin/bashtest#Return to normal for user test 3.usermod-d

What does linux ftp 530 mean? What does linux ftp 530 mean? Mar 14, 2023 am 10:16 AM

linux ftp530 means linux ftp login error 530. The solution is: 1. Check "cat /etc/shells" to see if your user's home directory and login shell are there. If not, add them; 2. Check "/var /log/secure" file and reset the password expiration time.

See all articles