PHP上传文件超过文件最大限制罗致无法上传成功
PHP上传文件超过文件最大限制导致无法上传成功
最近在学习《HeadFirst PHP & MySQL》一书的第5章“使用存储在文件中的数据”,做一个文件上传的应用时,出现了错误,就是文件无法成功上传。这个问题困扰了我很久,不过还好最后终于解决了。原因是我上传的图片文件大小超过了HTML 表单中MAX_FILE_SIZE 选项指定的值32768Bytes即32KB导致无法上传成功。
我使用了XAMPP(Apache + MySQL + PHP + Perl)集成开发包和Zend Studio 10.6作为PHP IDE开发环境,另外关于PHP调试我采用了XDebug,在Zend Studio10.6中配置Xdebug的PHP调试环境我参考了博文Zend Studio 10.5 与 XDebug 调试| Zend Debugger 说明 Drupal 源代码 (一)一文。
相应的文件上传示例PHP代码addscore.php如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Guitar Wars - Add Your High Score</title> <link rel="stylesheet" type="text/css" href="style.css" /></head><body> <h2 id="Guitar-Wars-Add-Your-High-Score">Guitar Wars - Add Your High Score</h2><?php require_once 'appvars.php'; require_once 'connectvars.php'; if (isset($_POST['submit'])) { // Grab the score data from the POST $name = $_POST['name']; $score = $_POST['score']; $screenshot = $_FILES['screenshot']['name']; if (!empty($name) && !empty($score) && !empty($screenshot)) { // Move the file to the target upload folder $target = GW_UPLOADPATH . $screenshot; echo json_encode($_FILES); if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) { // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error Connecting to MySQL Database!'); // Write the data to the database $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score','$screenshot')"; mysqli_query($dbc, $query) or die('Error querying database;'); // Confirm success with the user echo '<p>Thanks for adding your new high score!</p>'; echo '<p><strong>Name:</strong> ' . $name . '<br />'; echo '<strong>Score:</strong> ' . $score; echo '<img src="/static/imghw/default1.png" data-src="/img/2014/07/09/143017574.jpg" class="lazy" alt="Score image" /></p>'; echo '<p><a href="index.php"><< Back to high scores</a></p>'; // Clear the score data to clear the form $name = ""; $score = ""; $screenshot = ""; mysqli_close($dbc); } } else { echo '<p class="error">Please enter all of the information to add your high score.</p>'; } }?> <hr /> <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="MAX_FILE_SIZE" value="32768" /> <label for="name">Name:</label> <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br /> <label for="score">Score:</label> <input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" /><br /> <label for="screeshot">ScreenShot</label> <input type="file" id="screenshot" name="screenshot" /> <hr /> <input type="submit" value="Add" name="submit" /> </form></body> </html>
在使用Zend Sutdio10.6设置断点并调试上面这段PHP代码时我发现“if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {”这行代码里面的代码块没有执行,于是查看了超全局变量$_FILES['screenshot']['tmp_name']的值为空,然后我在这行代码前以JSON格式打印出$_FILES变量的值,如下:
{"screenshot":{"name":"Penguins.jpg","type":"","tmp_name":"","error":2,"size":0}}
相应的运行截图如下:
然后我查询$_FILES['screenshot']['error']为2,上网查询了一下,关于$_FILES超级全局变量的介绍大体如下:
另外,查询PHP参考手册关于move_uploaded_file函数的介绍如下:
move_uploaded_file(PHP 4 >= 4.0.3, PHP 5)move_uploaded_file — 将上传的文件移动到新位置说明bool move_uploaded_file ( string $filename , string $destination )本函数检查并确保由 filename 指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由 destination 指定的文件。 这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话。 参数filename上传的文件的文件名。 destination移动文件到这个位置。 返回值成功时返回 TRUE。 如果 filename 不是合法的上传文件,不会出现任何操作, move_uploaded_file() 将返回 FALSE。 如果 filename 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作, move_uploaded_file() 将返回 FALSE。此外还会发出一条警告。
范例
Example #1 Uploading multiple files
<?php$uploads_dir = '/uploads';foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "$uploads_dir/$name"); }}?>
...
}代码块没有执行。

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

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

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

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

screenshot is a folder dedicated to game screenshots. You can use F12 to take screenshots. The default is bmp format. The bmp format is a standard image file format in the Windows operating system and can be supported by a variety of Windows applications.

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Everyone is looking forward to today's Windows 1123H2 release. In fact, Microsoft has just launched updates to the release preview, which is the closest channel before the official release stage. Known as Build 22631, Microsoft said they are rolling out the new rebranded chat app, phone link, and play together widgets that have been tested on other internal channels over the past few months. "This new update will have the same servicing branch and codebase as Windows 11 version 22H2 and will be cumulative with all newly announced features, including Copilot in Windows (preview)," Microsoft promises. Redmond officials further
