亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) 用PHP實(shí)現(xiàn)文件上傳二法

用PHP實(shí)現(xiàn)文件上傳二法

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

上傳

PHP(Hypertext Preprocessor)是一種 HTML 內(nèi)嵌式的語言 (類似 IIS 上的ASP)。而 PHP 獨(dú)特的語法混合了 C、Java、Perl 以及 PHP 式的新語法。它可以比 CGI 或者 Perl 更快速的執(zhí)行動(dòng)態(tài)網(wǎng)頁。除此之外,用 PHP 寫出來的 Web 后端CGI 程序,可以很輕易的移植到不同的系統(tǒng)平臺(tái)上。

  我們?cè)谧鼍W(wǎng)站時(shí),需要訪問者的參于才能將網(wǎng)站建設(shè)得更加引人注目,這就要求我們從訪問者那里得到文章、圖片等。因此,文件上傳成為網(wǎng)頁中必不可少的功能?,F(xiàn)在我就利用現(xiàn)在流行的編程語言PHP,用兩種方法來說明其功能的實(shí)現(xiàn)。

一、利用php的文件函數(shù)來實(shí)現(xiàn)上傳
  這段代碼分為兩個(gè)文件,一個(gè)為upload.html,一個(gè)是upload.php。


上傳文件選擇:upload.html 代碼如下:


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

  

上載文件表單

  

  


    

  
請(qǐng)選擇文件:

      

    

  


  


  

  


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

*** 說明 ***

  1、請(qǐng)注意
這是一個(gè)標(biāo)簽,我們要實(shí)現(xiàn)文件的上傳,必須指定為 multipart/form-data ,否則服務(wù)器將不知道你在干什么!

  2、值得注意的是文件upload.html中表單選項(xiàng) MAX_FILE_SIZE 的隱藏值域,通過設(shè)置

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


處理剛剛上傳的文件:upload.php 代碼如下:


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

  

  

  處理上載文件

  

  

  

  copy($userfile,"newfile");

  echo $userfile."-用戶上傳到服務(wù)器上的文件臨時(shí)存放的名稱
";

  echo $userfile_name."-在用戶機(jī)器上該文件的原始名稱
";

  echo $userfile_size."-上傳文件的實(shí)際字節(jié)數(shù)
";

  echo $userfile_type."-如果用戶的瀏覽器提供了這個(gè)信息的話,它表示mime的類型。例如image/gif
";

  ?>

  

  


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

*** 說明 ***

  1、使用了PHP文件函數(shù)copy()將上載到臨時(shí)目錄下的文件拷貝到一個(gè)特定的目錄下,并重新命名為"newfile"。

  2、在upload.html中定義了一個(gè)變量 userfile,在upload.php中,我們就可以使用這個(gè)變量,直接通過$userfile訪問上載的文件,但由于這是一個(gè)保存文件的變量,因此文件名字必須通過另外一個(gè)$userfile_name變量取得。

  下面是這個(gè)變量的具體用法:

  $userfile:在將要存放上載文件的服務(wù)器上的臨時(shí)文件名字。

  $userfile_name:在發(fā)送者系統(tǒng)中的初始文件名。

  $userfile_size:按字節(jié)計(jì)算的上載文件的大小。

  $userfile_type:多用途網(wǎng)際郵件擴(kuò)充協(xié)議類型的文件,前提是瀏覽器提供這種信息,比如說“image/gif"。



二、利用FTP功能進(jìn)行文件上傳
  這段代碼同樣分為兩個(gè)文件,一個(gè)為upload.php,一個(gè)是ftp.php。


設(shè)置ftp的相關(guān)選項(xiàng)及選擇上傳文件名:upload.php 代碼如下:


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

  
  $username="用戶名";

  $password="用戶密碼";

  $server="主機(jī)名";

  $cdir="上傳目錄名" ;

//以上設(shè)置你的FTP主機(jī)名、用戶名和用戶密碼

  ?>



  




    >

    >

    >

    >

  

  

    

  

  

    

  

  
上傳文件選擇

    

    




    

    


  



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


處理上傳文件:ftp.php 代碼如下:


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

  
//ftp聯(lián)接主機(jī)函數(shù)

  function connect()

  {

  global $server, $username, $password;

  $conn = ftp_connect($server);

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

  return $conn;

  }

//建立ftp聯(lián)接

  $result = connect();

  if ($action == "上傳")

  {

//用來改變ftp路徑

  ftp_chdir($result, $cdir);

//用來上傳指定的文件,同名并以二進(jìn)制位傳遞

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

// 判斷是否正確上傳

  if ($res_code == 1)

echo "上傳成功!";

  else

echo "上傳錯(cuò)誤!";

  }

// 關(guān)閉聯(lián)接

  ftp_quit($result);

  ?>


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

*** 說明 ***

  函數(shù)ftp_put(int ftp_stream, string remote_file, string local_file, int mode)用法

  返回值: 布爾值

  本函數(shù)用來上傳指定的文件。參數(shù) ftp_stream 為 FTP 的連接代碼。參數(shù) remote_file 為欲存在遠(yuǎn)端的文件名。參數(shù) local_file 為欲上傳文件的文件名。參數(shù) mode 的值有 FTP_ASCII 及 FTP_BINARY 二種,分別表示文檔或者是二進(jìn)位文件。成功則返回 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1488
72
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

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

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

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 implement FTP file upload progress bar using PHP How to implement FTP file upload progress bar using PHP Jul 30, 2023 pm 06:51 PM

How to use PHP to implement FTP file upload progress bar 1. Background introduction In website development, file upload is a common function. For the upload of large files, in order to improve the user experience, we often need to display an upload progress bar to the user to let the user know the file upload process. This article will introduce how to use PHP to implement the FTP file upload progress bar function. 2. The basic idea of ??implementing the progress bar of FTP file upload. The progress bar of FTP file upload is usually calculated by calculating the size of the uploaded file and the size of the uploaded file.

See all articles