用PHP實(shí)現(xiàn)文件上傳二法
Jun 21, 2016 am 09:13 AM上傳
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 代碼如下:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
*** 說明 ***
1、請(qǐng)注意
--------------------------------------------------------------------------------
處理上傳文件: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 值。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

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

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

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

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: 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

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 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.
