I have a PHP script that allows me to send a pdf file to it and it saves the PDF file locally.
<?php
define('UPLOAD_DIR', 'upload/');
$img = $_POST['image'];
$img = str_replace('data:image/pdf;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . 'image.pdf';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
How do I run a function once the file has been saved?
Related
i want upload image from base64 png and rename it with different image type.
if ($img) {
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $name . = str_replace("image/", ".", $type);
$success = file_put_contents($file, $data);
}
Try with file_get_contents it has built in base64 data uri protocol wrapper:
if ($img) {
$contents = file_get_contents($img); // $img = 'data:image/png;base64,....'
// setting $file name etc.
$success = file_put_contents($file, $contents);
}
I use move_uploaded_file to upload my image to server. But when I check on my server, I found out that my image have move another file. I didn't spot any error on my code with online example. Can anyone guide me to solve this problem? TQ.
$file_path = realpath("../media/rewardreceipt/");
$img=$_POST['img'];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$img_name = uniqid();
$file_name = $img_name . '.jpeg';
$file = $file_path. '/' . $file_name;
$success = file_put_contents($file_path , $data);
if($success) {
$arr = array('result' =>$file_name);
echo json_encode($arr);
} else{
$arr = array('result' =>'false');
echo json_encode($arr);
}
i have a function which can write image on server folder with PHP. i follow instruction on this link Can't save a HTML5 Canvas as Image on a server , my problem is, i want to show success message when image save. on my code, it just show message only when write image failed. here is my function :
$loc = $_POST['lokasi'];
define('UPLOAD_DIR', $loc);
$filename = $_POST['nama-file'];
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR ;
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
thanks for your response. :)
$loc = $_POST['lokasi'];
define('UPLOAD_DIR', $loc);
$filename = $_POST['nama-file'];
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR ;
if (file_put_contents($file, $data)) {
echo "Image saved";
} else {
echo "Unable to save the file.";
}
Note: With file-uploads its better to use move_uploaded_file
I am sending an xhr base64 encoded string from JavaScript to a php file. In php I get the content and put it in a file:
<?php
$img = trim($_POST["base64"]);
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
file_put_contents('image.png', $data);
echo 'image.png';
?>
How can I create a unique file name, instead of file_put_contents in image.png, set it into a /tmp folder and return the unique URL?
Thanks you.
Im trying to convert base64 image to png.This is my code
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
The file is created successfully. But it could not be open.Its not in a readable format.
Sorry for reviving your question, but take a look, it worked like charm:
<?php
$img = $_POST['Base64Variable'];
define('UPLOAD_DIR', 'YourFolder/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Could not save the file!';
?>