簡體   English   中英

如何在php變量中的服務器上存儲base64映像?

[英]How to Store base64 image on server which is in php variable?

我正在嘗試將base64映像上載到服務器,因此我將其轉換為映像,但未在服務器上上載。 我在上傳圖片時遇到的錯誤是:

注意:/home/dev/public_html/wp-content/plugins/paypal-frontend-registration/advertise_step_three.php在第45行{“ success”:0,“ message”:“服務器錯誤。未定義的索引:584fb34801024.png無法上傳。“}

這是我正在嘗試的代碼:

$sign_img=$_POST['hdn_sign_img'];
$sign_img=str_replace('\"','',$sign_img);
$imageData = $sign_img;
list($type, $imageData) = explode(';', $imageData);
list(,$extension) = explode('/',$type);
list(,$imageData)      = explode(',', $imageData);
$fileName = uniqid().'.'.$extension;
$imageData = base64_decode($imageData);

$data = file_put_contents($fileName, $imageData);
var_dump($data);

$target = '/home/dev/public_html/wp-content/uploads';
$result = move_uploaded_file( $_FILES[$fileName], $target);

if($result){
    $response["success"] = 1;
    $response["message"] = "Upload Successful.";
    die(json_encode($response));
}else{
    $response["success"] = 0;
    $response["message"] = "Server error. Could not upload.";
    die(json_encode($response));
}

如果您確實想將其存儲在文件中而不使用最常用的PHP上傳方法,則可以使用file_put_contents()

file_put_contents('/path/to/filename.ext', $imageData);

和瞧。 它已保存到您的服務器。

來自PHP.net的示例

這是move_uploaded_file()的示例

$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        // basename() may prevent filesystem traversal attacks;
        // further validation/sanitation of the filename may be appropriate
        $name = basename($_FILES["pictures"]["name"][$key]);
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

一些閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM