我有一個這樣的輸入:
<input type="file" accept="image/*">
現在我想將影像發送到服務器(我猜 ajax 是要走的路?)
從服務器我想將影像保存到 aws-s3 存盤(實際上不是我的問題)
問題是我如何發送以某種方式將影像轉換為 php,以便我以后可以將其存盤在物件存盤中?
uj5u.com熱心網友回復:
此代碼是從以下網頁復制的: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 上傳影像的方式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/384252.html
標籤:javascript php 查询 阿贾克斯 亚马逊-s3
