2017-05-29 36 views
-1

在項目中,我目前正在爲一家公司工作,我需要將圖像發送到服務器上的.php文件,然後將其保存在文件夾中,然後發回url我可以將它保存在數據庫的桌子上。公司希望我發送原始圖像,而不是將其轉換爲base64,發送並將其解碼爲.php文件。直接發送圖像到.PHP而不使用Base64

我的問題是,這可能嗎?如果是這樣,我該怎麼做?

感謝您的幫助。

+0

我很確定如果你在這裏搜索,你會發現許多類似的答案 – RiggsFolly

回答

0

是的,這是可能的。

檢查您的php.ini文件,並確保這條線是這樣的:

file_uploads = On 

創建HTML表單:

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

開創全省upload.php的文件:

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = 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 ". basename($_FILES["fileToUpload"]["name"]). " has been  uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 

爲了確保一切正常,請創建一個名爲uploads的文件夾,並確保文件權限和所有權都可以。

希望它可以幫助你。

+0

請注意,這段代碼來自[w3schools](https://www.w3schools.com/php/php_file_upload.asp) –