Attach image in a php form and use html2pdf - php

i have a php form and use html2pdf (TCPDF). The php form works when I use text and images that already include in the form, but now I want to attach a image and also include that in the pdf file.
The code I am using now I get the filename.
To attach the image I am using
$<span><input type="file" name="foto"></span>
And to include it in the pdf-file I use this code
$foto = $_POST['foto'];
$content .= '<img src='.$foto.'with=200 height=auto>';
Sorry for my bad English!
Regards
Daniel

Do an normal HTML file upload like this:
<form method="post" enctype="multipart/form-data">
<label>Deine Datei ;) :
<input name="fileUpload" type="file" accept="image/*">
</label>
<label>Your Name:
<input name="userName" type="text" accept="image/*">
</label>
<button type="submite" name="submite">Upload</button>
</form>
And on the PHP side you must move your file:
if(isset($_POST["submit"])) {
$userName = $_POST['userName']; // the normale methode
$check = getimagesize($_FILES["fileUpload"]["tmp_name"]);
if($check !== false) {
$temp_path_to_file = $_FILES['fileUpload']['tmp_name'];
$file_content = file_get_contents($temp_path_to_file);
// and now past your image to your PDF.
// if you want to save the image on the server you must move it with this (its not necessary if you want only to past it in your PDF)
if(move_uploaded_file($temp_path_to_file, '/my/image/folder/image_'.uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION);
}
}
EDIT:
For your example you must convert the $file_content to an base64 string. Than you can past it in your image:
$base64 = 'data:image/' . pathinfo($filename, PATHINFO_EXTENSION) . ';base64,' . base64_encode($file_content);
echo '<img src="'. $base64 .'" style="with: 200; height: auto">';

Related

How to show only .pdf file when browse file to upload using Laravel 5.0?

I am using Laravel 5.0. I have a file upload function. It's going well. But I want to make the user only uploaded pdf files. I want when browse a file, the storage only show files with only .pdf files. And I want to have a size limit to file which want to upload.
Here is the view
<div class="form-group">
<label for="upload_file" class="control-label col-sm-2">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file" required>
</div>
</div>
Here is the controller
$destination = 'files';
if($request->hasFile('upload_file')) {
$file = $request->file('upload_file');
$extension = $file->getClientOriginalExtension();
$file_name = str_replace('/','_',$request['nomor_surat']) . '-' . $kode_divisi[0]->kode_divisi . '.' . $extension;
$file->move($destination, $file_name );
}
$upload_file = $file_name;
$surat = new Surat();
$surat->upload_file = $upload_file;
$surat->save();
and what should I do next? what should I add in my controller and route and view?
You can limit the user to browse PDF files only with the HTML file input accept attribute:
<input type="file" accept="application/pdf" />
To limit the size, you can do it on the server side:
$file = $request->file('upload_file');
//no files larger than 700kb
if ($file->getSize() > 700000)
{
//respond not validated, file too big.
}
You can also validate the file type on the server as well:
if ($file->getClientMimeType() !== 'application/pdf')
{
//respond not validated, invalid file type.
}

Display an image on the page without uploading to server

Is this possible to display an image on the currunt browser session without uploading it to the server?
I am trying to display an image on my php page when users enter the upload button ,the uploaded image will temprory appear on the target url.
Here is my html form
<form action="demo.php"method="post"enctype="multipart/form-data">
<input type="file"name="uf">
<input type="submit"value="upload">
</form>
And the target file :
<?php
$pic=$_FILES["uf"]["name"];
echo "<img src='$pic'>";
It doesnt display the image. So is this somehow possible to display the image the way I am doing it?
Any help is much appriciated!
THANKS!
Here you go: http://jsfiddle.net/LvsYc/
html:
<form method="post" enctype="multipart/form-data">
<input type="file" id="imgInp" />
<img id="blah" src="#" alt="your image" />
</form>
js (requires jQuery):
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
You can convert the image into base64, see http://www.motobit.com/util/base64/css-images-to-base64.asp .
It will generate an <img /> like this: <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAA// lot more characters" />
It is, in my mind, a bad practice, since your browser cache the images in order to avoid to send lots of requests for an image he already has.
In php:
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
(taken from) How to convert an image to base64 encoding?

php wont give download link feedback after upload done

i'm trying to get DropzoneJS to work, i got far but it seems i've got a problem i can't solve.
so basicly what i want is to upload a file and get a download link back within the same page.
so i added this code to my index.php:
<form action="index.php" class="dropzone" id="my-awesome-dropzone"></form>
and the php code into the index.php:
<?php
include('config.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$dirname = uniqid();
mkdir( $filedir.'/'.$dirname);
mkdir( $filedir.'/'.$dirname.'/file');
copy("template/d.php", "d/$dirname/index.php");
copy("template/dl.php", "d/$dirname/dl.php");
preg_match('/\.([a-zA-Z0-9]+?)$/', $_FILES['file']['name'], $matches);
if(in_array(strtolower($matches[1]), $accepted)) {
if($_FILES['file']['size'] <= $maxsize) {
$newname = ($_FILES['file']['name']);
// file name encryption
//$newname = md5_file($_FILES['file']['tmp_name']).'.'.$matches[1];
move_uploaded_file($_FILES['file']['tmp_name'], $filedir.'/'.$dirname.'/file/'.$newname);
$linkurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).'#'.$newname;
$imgurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$filedir.'/'.$dirname.'/file/'.$newname;
$alt = $_POST["alt"];
$dlurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace('/\/([^\/]+?)$/', '/', $_SERVER['PHP_SELF']).$filedir.'/'.$dirname;
print '<h2>Picture Uploaded Successfuly!!!!</h2> <p id="codes">
<img src="'.$imgurl.'" height="300" alt="Uploaded Picture" >
<label for="codebb">BBCode:</label>
<input type="text" id="codebb" value="[URL='.$siteurl.'][IMG]'.$dlurl.'[/IMG][/URL]" onclick="javascript:this.focus();this.select();" readonly="true" /><br />
<label for="codehtml">HTML Code: </label>
<input type="text" id="codehtml" value=\'<a href="'.$siteurl.'"><img src="'.$dlurl.'" alt="'.$alt.'" />&lt/a>\' onclick="javascript:this.focus();this.select();" readonly="true" /><br />
<label for="codedirect">Direct Link:</label>
<input type="text" id="codedirect" value="'.$dlurl.'" onclick="javascript:this.focus();this.select();" readonly="true" /></p>';
echo ".$newname";
} else
print '<p>Sorry, Max Picture size is 10Mb</p>';
}
}
?>
when i upload the file the php works and the file is uploaded successfully into its folder so the php is working.
my problem now is why dont i get the print from the php script.
fully working script:
http://37.34.62.131/test/uploader%201.0/
not working NEW script:
http://37.34.62.131/test/
i think it has something to do with the uploader because my old script uses a submit button and my newer version is using a automatic submit.
i very appreciate your time!

Empty Image was saved on server

i need your help...i want to copy/upload images on server and saving directories in database...Problem is the images are empty =(
I'm using:
WampServer
Apache Version : 2.2.22
PHP Version : 5.3.13
MySQL Version : 5.5.24
Issue is that it's creating the file but is empty.... can it be WampServer fault?
CODE:
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
<input name="image" id="image" multiple="true" type="file" />
<input name="name" id="name" type="text" maxlength="50" value="" placeholder="Enter Image Name" class="text-field"/>
if(isset($_POST['name']) && isset($_POST['image']))
{
$name = $_POST['name'];
$img = $_POST['image'];
$file_url = $img;
$fp = fopen($file_url, 'rb');
$content = fread($fp, filesize($file_url));
$fp = fopen('../Images/UploadedImages/'.$img, 'wb');
$image='../Images/UploadedImages/'.$img;
$result=mysql_query("insert into Images(Name,Directory,Register_Day)
values ('$name','$image',now())");
if (!$result) {
die("Failed to load");}
else{
fputs($fp, $content);
fclose($fp);
}
Uloading images doesn't work like that, you must do something like this:
$img = $_FILES['image']['tmp_name'];
Also note that you don't want to save the image into the database, you (probably) want to save the image to the server but you save only the file location into the database.
You cant upload image by using fopen. Try below code.
Your will get image details in $_FILES array not in $_POST.
$tmp_name = $_FILES['image']['tmp_name'];
$name = $_FILES['image']["name"];
$uploads_dir = '../Images/UploadedImages';
move_uploaded_file($tmp_name, "$uploads_dir/$name");
In above code $uploads_dir/$name will be your image path which you can store in db.
And also make sure that your <form> tag has enctype="multipart/form-data" attribute set.

Is it possible to send additional parameters when uploading a file in PHP?

I am initializing file upload using the following HTML:
<form enctype="multipart/form-data" action="PHPScripts/upload.php" method="POST">
<input type="file" id="browseButton" name="image" onchange="this.form.submit();" />
</form>
Upload.php script looks like this:
<?php
$file = $_FILES["image"];
$filepath = $file["name"];
$filetmp = $file["tmp_name"];
$filesize = $file["size"];
$filename = basename($filepath);
$filetype = substr($filename, strrpos($filename, ".") + 1);
...
?>
I need to pass one more parameter to my php script, but I don't know how. HTTP method is POST (as can be seen in the code above), but where should I put the parameter? Is that even possible? Thanks for clarifying this to me.
Just add another input element of your choice. No additional magic required.
<input type="hidden" name="info" value="Test">
...
$info = $_POST["info"];
Just put one element inside the same form where the file input is?

Categories