Wie behebe ich ein nicht funktionierendes Move_uploaded_file() in PHP?

Patricia Arquette
Freigeben: 2024-10-18 14:18:03
Original
671 Leute haben es durchsucht

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>
Nach dem Login kopieren

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.

Das obige ist der detaillierte Inhalt vonWie behebe ich ein nicht funktionierendes Move_uploaded_file() in PHP?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!