- Base64 encode uploaded file then save in database
- 1 Answer 1
- Преобразование изображения в Base64 на PHP
- Результат:
- php image file upload and convert to base64 without saving image
- How to encode a PHP file with base64
- 2 Answers 2
- Related
- Hot Network Questions
- Subscribe to RSS
- How to Use Base64 Encoding in PHP
- What Is Base64 Encoding?
- Base64 Functions in PHP
- base64_encode
- base64_decode
- How to Encode and Decode Data With MIME Base64
- Encode URL Parameters
- Base64-Encoded Checksum
- Email Attachments
- Conclusion
Base64 encode uploaded file then save in database
I want to base 64 encode uploaded file then save this into Blob type column of a table in MySQL database. I have tried built in PHP function base64_encode with file variable but this does not seem to work. Is there a way we can do this? The reason is that I do not want to use moveuploaded file into a folder. Thank you.
«The reason is that I do not want to use moveuploaded file into a folder.» — actually it is not the reason. Reason is what caused that
1 Answer 1
As @Sjoerd and @zerkms correctly point out, you do not need to do this — a blob column is be used to store raw binary data, so you can bypass the Base64 process and insert the raw data of the file.
If you want to store images in a database (which, by the way, I personally don’t like to do) it is better to store the raw data — Base64 encoding the data makes it larger, and means that it must be decoded before the image is rendered, which adds processing overhead.
This is how you can insert the raw binary data (assuming the MySQL extension):
$data = file_get_contents($_FILES['name_of_control']['tmp_name']); $data = mysql_real_escape_string($data); $query = " INSERT INTO table (`blob_column`) VALUES ('$data') "; mysql_query($query);
If you really do want to Base64 encode it (in which case it could just be stored in a varchar), just just add a base64_encode() call:
$data = file_get_contents($_FILES['name_of_control']['tmp_name']); $data = base64_encode($data); // There is an argument that this is unnecessary with base64 encoded data, but // better safe than sorry :) $data = mysql_real_escape_string($data); $query = " INSERT INTO table (`varchar_column`) VALUES ('$data') "; mysql_query($query);
Преобразование изображения в Base64 на PHP
Преобразование файла в Base64-изображение для вставки тег или CSS background производится по следующему формату:
Для теста возьмем маленькое изображение: Для него в PHP нужно получить MIME, прочитать файл и закодировать полученные данные функцией base64_encode() . Т.к. функция getimagesize() не актуальна для SVG файлов, для них придется сделать исключение.
Результат:
![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E)
php image file upload and convert to base64 without saving image
I know how to upload image file and save to other location by using the following code. However, I need to do in such a way that user upload image and automatically convert to base64 without saving that image in my location. How should I do?
"; $type = pathinfo($file_tmp, PATHINFO_EXTENSION); $data = file_get_contents($file_ext); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo "Base64 is ".$base64; if(in_array($file_ext,$allowed_ext) === false) < $errors[]='Extension not allowed'; >if($file_size > 2097152) < $errors[]= 'File size must be under 2mb'; >if(empty($errors)) < if( move_uploaded_file($file_tmp, 'images/'.$file_name)); < echo 'File uploaded'; >> else < foreach($errors as $error) < echo $error , '
'; > > // print_r($errors); > ?>
Use file_get_contents() to get the contents of the temporary uploaded file, base64_encode() to encode it, file_put_contents() to save the file. Although storing a base64 encoded representation of an image file sounds like a bad idea — with big files, you may run into RAM problems, and the resulting file will be 33% larger than the original. Why do that?
Yes. I need to test with that one. If I writing like this, I can’t get base64 string. Can you please guide me? $type = pathinfo($file_tmp, PATHINFO_EXTENSION); $data = file_get_contents($file_ext); $base64 = ‘data:image/’ . $type . ‘;base64,’ . base64_encode($data); echo «Base64 is «.$base64;
How to encode a PHP file with base64
🙂 I have one ridiculously silly question and most of you would like to reffer me to Google right away, but that didn’t helped me out within the first hour. I suppose I didn’t knew how to look for. I’m having a PHP file and I’d like to have it in base64 yet I can’t get it to work anyhow. 1) I encoded my PHP script to base64(and included the PHP tags). It’ll look as following : JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ== This kind of base64 won’t execute so I added the PHP tags to it although the encoded file already had it. Still didn’t worked out. Removed the tags from the base64 and tried again, but still didn’t worked. Then I tried adding the PHP tags and inside of them added : eval(gzinflate(base64_decode(‘base64 here’))); Still didn’t worked out anyhow. Is anyone here kind enough to tell the kiddo how to run a base64 encoded PHP file properly? Would be really appreaciated. 🙂
2 Answers 2
$source = "JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ= JTNDJTNGcGhwJTIwVGhpcyUyMGlzJTIwdGhlJTIwUEhQJTIwY29kZSUyMCUzRiUzRQ= http://eaccelerator.net/" rel="noreferrer">this one or this one. They will crypt your codes and make them even faster!