php文件上传的两种实现方法

下面是关于php文件上传的两种实现方法的完整攻略。

下面是关于php文件上传的两种实现方法的完整攻略。

实现方法一:使用原生的PHP函数

使用原生的PHP函数可以实现文件上传,可以通过以下步骤来实现:

  1. 首先在前端页面中添加一个表单,其中包含一个file字段,用于选择文件。
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>
  1. 在上传文件的php脚本中,使用$_FILES超全局变量获取上传的文件信息。
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  1. 判断上传的文件是否是合法的文件类型。
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
  1. 判断上传的文件是否已经存在,如果存在就提示用户选择其他文件。
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
  1. 判断上传文件大小,如果超过规定大小,则提示用户文件太大。
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
  1. 执行最终的上传操作,如果上传成功,就提示用户文件已经上传成功,如果上传失败,就提示用户上传失败。
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

实现方法二:使用第三方库

使用第三方库也可以实现文件上传操作,可以通过以下步骤来实现:

  1. 首先,在项目目录下使用composer命令来安装第三方库。
composer require symfony/http-foundation
  1. 在上传文件的php脚本中,使用这个第三方库来获取上传的文件信息。
$request = Request::createFromGlobals();
$file = $request->files->get('fileToUpload');
  1. 使用这个第三方库的上传函数来执行上传操作。
$target_dir = "uploads/";
$target_file = $target_dir . $file->getClientOriginalName();
$file->move($target_dir, $target_file);
echo "The file ". basename( $file->getClientOriginalName()). " has been uploaded.";

示例1:

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();
$file = $request->files->get('fileToUpload');

$target_dir = "uploads/";
$target_file = $target_dir . $file->getClientOriginalName();
$file->move($target_dir, $target_file);
echo "The file ". basename( $file->getClientOriginalName()). " has been uploaded.";
?>

示例2:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

希望这些步骤对您有所帮助!

本文标题为:php文件上传的两种实现方法

基础教程推荐