PHP로 개발된 이미지 업로드 기능의 전체 소스 코드 표시

<?php
if (in_array($_FILES["file"]["type"], array("image/gif", "image/jpeg","image/jpg"))) {
    if ($_FILES["file"]["error"] > 0)                         //由文件上传导致的错误提示代码
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " .  $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],                   // 存储在服务器的文件的临时副本的名称
                "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}
?>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<body>
<form action="upload.php" method="post"
      enctype="multipart/form-data">
    <label for="file">文件名:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="上传" />
</form>
</body>
</html>

두 번째 방법:

<?php
if (count($_FILES)) {
    $retFiles = uploadFile("file");
    if ($retFiles['status'] == 1) {
        echo $retFiles['message'];
    } else if ($retFiles['status'] == 0) {
        echo "Upload: " . $retFiles["name"] . "<br />";
        echo "Type: " .  $retFiles["type"] . "<br />";
        echo "Size: " . ($retFiles["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $retFiles["temp"] . "<br />";
        echo "Stored in: " . "upload/" . $retFiles["name"];
    }
}
function uploadFile($fileName)
{
    $files = $_FILES[$fileName];
    if (!in_array($files["type"], array("image/gif","image/jpeg","image/pjpeg","image/jpeg", "image/png"))) {
        return array("status" => 1, "message" => "Uploaded image format is not correct");
    }
    
    if ($files["error"] > 0) {
        return array("status" => 1, "message" => "upload error");
    }
    if (file_exists("upload/" . $files["name"])) {
        return array("status" => 1, "message" => $files["name"]." already exiss");
    }
    move_uploaded_file($files["tmp_name"], "upload/".$files["name"]);
    return array("status" => 0, "name" => $files["name"], "type" => $files["type"], "size" => $files['size']/1024, "temp" => $files["tmp_name"]);
}
?>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<body>
<form action="upload.php" method="post"
      enctype="multipart/form-data">
    <label for="file">文件名:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="上传" />
</form>
</body>
</html>


지속적인 학습
||
<?php if (count($_FILES)) { $retFiles = uploadFile("file"); if ($retFiles['status'] == 1) { echo $retFiles['message']; } else if ($retFiles['status'] == 0) { echo "Upload: " . $retFiles["name"] . "<br />"; echo "Type: " . $retFiles["type"] . "<br />"; echo "Size: " . ($retFiles["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $retFiles["temp"] . "<br />"; echo "Stored in: " . "upload/" . $retFiles["name"]; } } function uploadFile($fileName) { $files = $_FILES[$fileName]; if (!in_array($files["type"], array("image/gif","image/jpeg","image/pjpeg","image/jpeg", "image/png"))) { return array("status" => 1, "message" => "Uploaded image format is not correct"); } if ($files["error"] > 0) { return array("status" => 1, "message" => "upload error"); } if (file_exists("upload/" . $files["name"])) { return array("status" => 1, "message" => $files["name"]." already exiss"); } move_uploaded_file($files["tmp_name"], "upload/".$files["name"]); return array("status" => 0, "name" => $files["name"], "type" => $files["type"], "size" => $files['size']/1024, "temp" => $files["tmp_name"]); } ?> <html> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no" /> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="上传" /> </form> </body> </html>
  • 코스 추천
  • 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~