如何解決 PHP 中無法執行的 Move_uploaded_file() 問題?

Patricia Arquette
發布: 2024-10-18 14:18:03
原創
671 人瀏覽過

How to Troubleshoot a Non-Functioning Move_uploaded_file() in PHP?

Troubleshooting the Move_uploaded_file() Function

When uploading files through a web application, it's essential to ensure that the move_uploaded_file() function operates flawlessly. However, users may encounter instances where it seems not to work as expected.

In this article, we'll delve into the problem of move_uploaded_file() not working and provide solutions to address it.

Problem Overview

The user has implemented a PHP script that utilizes the move_uploaded_file() function to handle file uploads. However, despite replicating the code provided in tutorials, the function fails to function properly.

Code Analysis

The HTML code for the form and upload script appears to be correct. It includes necessary attributes for the file input and submits the data to the upload_file.php script.

In upload_file.php, the script correctly extracts information about the uploaded file, including its name, temporary location, size, and error code. However, the move_uploaded_file() function is incorrectly used, leading to the failure.

Solution

To resolve the issue, two steps are crucial:

  1. Enable PHP Error Reporting:
    This allows PHP to display detailed error messages, which can assist in identifying the root cause of the problem.
  2. Check the Error Code:
    The $_FILES'image' variable contains an error code indicating the reason for the failure. Common error codes include:

    • UPLOAD_ERR_OK: No error
    • UPLOAD_ERR_INI_SIZE: File size exceeds the maximum allowed by the server
    • UPLOAD_ERR_FORM_SIZE: File size exceeds the maximum allowed by the form
    • UPLOAD_ERR_NO_FILE: No file was uploaded
    • UPLOAD_ERR_PARTIAL: File was only partially uploaded

Final Corrected Code

Upon addressing these issues, the corrected code looks like this:

<code class="php">
<?php
$move = __DIR__.'/../../uploads/';
echo $_FILES["file"]['name']."<br>";
echo $_FILES["file"]['tmp_name']."<br>";
echo $_FILES["file"]['size']."<br>";
echo $_FILES['file']['error']."<br>";
move_uploaded_file($_FILES['file']['tmp_name'], $move);
?>
</code>
登入後複製

In this updated code, the temporary file name (tmp_name) is used instead of the original file name (name), which ensures that the file is correctly moved to the desired location.

以上是如何解決 PHP 中無法執行的 Move_uploaded_file() 問題?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!