简体   繁体   English

使用 JQuery/Ajax 将图像从输入上传到服务器

[英]Upload an image from input to server using JQuery/Ajax

I have an Input like this:我有这样的输入:
<input type="file" accept="image/*">

Now I want to send the image to the server (I guess ajax is the way to go?)现在我想将图像发送到服务器(我猜 ajax 是 go 的方式?)
From the Server I want to save the image to an aws-s3 storage (not actually my problem)从服务器我想将图像保存到 aws-s3 存储(实际上不是我的问题)
The question is how do I send the image to php in a way that I can later store it in an object storage?问题是我如何将图像发送到 php 以便以后可以将其存储在 object 存储中?

This code was copied from the following web page: https://www.w3schools.com/PHP/php_file_upload.asp此代码是从以下 web 页面复制的: https://www.w3schools.com/PHP/php_file_upload.asp

Note that it's much more harder to use AJAX/jQuery, so you can use this code.请注意,使用 AJAX/jQuery 更加困难,因此您可以使用此代码。

First check your php.ini file (it's in C:/php-install-path/php.ini) and search for the following line:首先检查您的 php.ini 文件(位于 C:/php-install-path/php.ini 中)并搜索以下行:

file_uploads = On

It may appear as它可能显示为

file_uploads = Off

so you need to turn in to On .所以你需要打开On Then restart your web server if it was off.然后重新启动您的 web 服务器(如果它已关闭)。

Next, create the form.接下来,创建表单。

<!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>

It will need to redirect to a PHP file as PHP can receive elements.它将需要重定向到 PHP 文件,因为 PHP 可以接收元素。

For the PHP file, put code like this:对于 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.";
  }
}
?>

Bonus: If you want to create a function for this, you can.奖励:如果你想为此创建一个 function,你可以。

<?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.";
    }
  }
}
?>

Then include or require the file in the PHP file that's receiving the file upload.然后在接收文件上传的 PHP 文件中包含或要求该文件。

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

There you go.那里有 go。 That's how you use PHP to upload an image.这就是您使用 PHP 上传图像的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM