【问题标题】:Upload an image from input to server using JQuery/Ajax使用 JQuery/Ajax 将图像从输入上传到服务器
【发布时间】:2022-01-19 20:42:09
【问题描述】:

我有这样的输入:
<input type="file" accept="image/*">

现在我想将图像发送到服务器(我想 ajax 是要走的路?)
我想从服务器将图像保存到 aws-s3 存储(实际上不是我的问题)
问题是如何将图像发送到 php,以便以后将其存储在对象存储中?

【问题讨论】:

    标签: javascript php jquery ajax amazon-s3


    【解决方案1】:

    此代码复制自以下网页:https://www.w3schools.com/PHP/php_file_upload.asp

    请注意,使用 AJAX/jQuery 更难,因此您可以使用此代码。

    首先检查您的 php.ini 文件(它在 C:/php-install-path/php.ini 中)并搜索以下行:

    file_uploads = On
    

    它可能显示为

    file_uploads = Off
    

    所以您需要转至On。如果 Web 服务器已关闭,请重新启动它。

    接下来,创建表单。

    <!DOCTYPE html>
    <html>
    <body>
    
    <form action="upload.php" method="post" enctype="multipart/form-data">
      Select image to upload:
      <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
    </form>
    
    </body>
    </html>
    

    它需要重定向到一个 PHP 文件,因为 PHP 可以接收元素。

    对于 PHP 文件,输入如下代码:

    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
      } else {
        echo "File is not an image.";
        $uploadOk = 0;
      }
    }
    
    // Check if file already exists
    if (file_exists($target_file)) {
      echo "Sorry, file already exists.";
      $uploadOk = 0;
    }
    
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
      echo "Sorry, your file is too large.";
      $uploadOk = 0;
    }
    
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
    }
    
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
      echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
      if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
      } else {
        echo "Sorry, there was an error uploading your file.";
      }
    }
    ?>
    

    奖励:如果你想为此创建一个函数,你可以。

    <?php
    function uploadFile($names, $button) {
      $file = $_FILES[$names];
      $target_dir = "uploads/";
      $target_file = $target_dir . basename($file["name"]);
      $uploadOk = 1;
      $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    
      // Check if image file is a actual image or fake image
      if(!empty($button)) {
        $check = getimagesize($file["fileToUpload"]["tmp_name"]);
        if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
      } else {
        echo "File is not an image.";
        $uploadOk = 0;
      }
    
      // Check if file already exists
      if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
      }
    
      // Check file size
      if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
      }
    
      // Allow certain file formats
      if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
      }
    
      // Check if $uploadOk is set to 0 by an error
      if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
      // if everything is ok, try to upload file
      } else {
        if (move_uploaded_file($file["fileToUpload"]["tmp_name"], $target_file)) {
          echo "The file ". htmlspecialchars( basename( $file["fileToUpload"] ["name"])). " has been uploaded.";
        } else {
          echo "Sorry, there was an error uploading your file.";
        }
      }
    }
    ?>
    

    然后在接收文件上传的 PHP 文件中包含或要求该文件。

    <?php
    include_once("file_upload_fn.php");
    uploadFile("fileToUpload", $_POST['submit']);
    ?>
    

    给你。这就是你使用 PHP 上传图片的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多