用PHP3实现文件上载
PHP3是一种非常强大的CGI脚本语言,就其语言特点基本上源于C,就其实现方式而言更象PERL,而其对数据库的内在支持更使之成为ASP的强大对手。
首先在前台必须有一个界面
文件上载界面
SubmitForm"
ACTION = "upload.php3" METHOD = "POST" >
"MAX_FILE_SIZE" VALUE ="1000000" >
UploadFile"
TYPE = "file" SIZE = "30" >
"提交" TYPE = "submit" >
"重置" TYPE = "reset" >
值得注意的是一个 MAX_FILE_SIZE 的隐藏值域,通过设置其VALUE 可以限制上载文件的大小。
然后在后台实现文件上载操作。如果只是最基本的上载,寥寥几行就可以搞定
If($UploadFile != "none")
{
copy($UploadFile,"$UploadFile_name");
unlink($UploadFile);
}
else
{
echo "你没有选择任何文件上载!";
}
? >
因为form传递过来的值自动赋值给同名变量,所以直接通过$UploadFile可以访问上载的文件,但由于这是一个保存文件的变量,因此文件名字必须通过另外一个$UploadFile_name变量取得。在拷贝文件后删去临时文件(unlink)。
上面的代码完全可以工作,但实际应用中漏洞百出,让我们逐步来完善之
首先,上载的文件必须有一个固定的目录保存,我们在这里用一个$UploadPath 变量保存之,如
$UploadPath = "/home/flier/upload/";
或复杂一点的自动定位,如
$UploadPath = AddSlashes(dirname
($PATH_TRANSLATED))."\\upload\\";
$PATH_TRANSLATED顾名思义是当前传送目录,
我们假定以其一个名为upload的子目录来保存上载的文件。dirname函数返回其目录名,然后加上子目录名
然后用一个变量$FileName保存完整的上载后文件名和路径$FileName = $UploadPath.$UploadFile_name;
其次,我们还想让用户得知上载文件的简要信息,如上载文件的大小
if($UploadFile_size // 上载文件大小
{
$FileSize = (string)$UploadFile_size . " 字节";
}
elseif($UploadFile_size {
$FileSize = number_format((double)
($UploadFile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)
($UploadFile_size / (1024 * 1024)), 1) . " MB";
}
number_format函数起到格式化输出的作用,具体用法请参照手册
下一步我们必须考虑到文件已经存在和拷贝操作失败的情况,并提供相应的提示信息
if(!file_exists($FileName))
{
if(copy($UploadFile,$FileName))
{
echo "文件 $UploadFile_name
($FileSize)上载成功!";
}
else
{
echo "文件 $UploadFile_name 上载失败!";
}
unlink($UploadFile);
}
else
{
echo "文件 $UploadFile_name 已经存在!";
}
然后我们应该考虑到大文件上载时容易出现超时的情况,可以用set_time_limit($TimeLimit); 加大超时限制时间。
最后,把截面和实现代码综合到一个单独的文件中,为了实现这个想法,我们通过在 form中添加一个隐含值UploadAction" VALUE = "1" > 指出当前的状态(界面或实现),以便区分对待 完成代码如下(文件名upload.php3)
if(!$UploadAction):
? >
文件上载界面
"SubmitForm" ACTION = "upload.php3" METHOD = "POST" >
"MAX_FILE_SIZE" VALUE ="1000000" >
UploadAction" VALUE = "1" >
UploadFile" TYPE =
"file" SIZE = "30" >
"提交" TYPE = "submit" >
"重置" TYPE = "reset" >
else:
? >
文件上载代码
$UploadAction=0;
$TimeLimit=60; /* 设置超时限制时间
缺省时间为 30 秒
设置为0时为不限时 */
set_time_limit($TimeLimit);
If(($UploadFile != "none")&&
($UploadFile != ""))
{
$UploadPath = AddSlashes(dirname
($PATH_TRANSLATED))."\\upload\\"; // 上载文件存放路径
$FileName = $UploadPath.$UploadFile_name;
// 上载文件名
if($UploadFile_size // 上载文件大小
{
$FileSize = (string)$UploadFile_size . " 字节";
}
elseif($UploadFile_size {
$FileSize = number_format((double)
($UploadFile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)
($UploadFile_size / (1024 * 1024)), 1) . " MB";
}
if(!file_exists($FileName))
{
if(copy($UploadFile,$FileName))
{
echo "文件 $UploadFile_name ($FileSize)上载成功!";
}
else
{
echo "文件 $UploadFile_name 上载失败!";
}
unlink($UploadFile);
}
else
{
echo "文件 $UploadFile_name 已经存在!";
}
}
else
{
echo "你没有选择任何文件上载!";
}
set_time_limit(30); // 恢复缺省超时设置
? >
返回
endif;

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

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

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
