What I'm attempting to accomplish
Hi! I've scoured the web and can't quite find what I'm looking for. We are working on an internal email signature creation tool. I would like to add an image to a session so I can insert that image into the tool at the end. I do not want to store it in a temp folder or save it to a database. I know this is not best practice to put images in a session. The tool copies all rendered HTML to your clipboard to be immediately inserted into Gmail/Outlook.
My code on Page 1
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
</form>
My code on Page 2
<?php
$_SESSION["fileToUpload"] = $_POST['fileToUpload'];
?>
The result
"image.png" text is inserted into the signature tool. Instead of the file info/name inserted into the tool, I want the actual image itself. Is this possible? Thanks!
So you're trying to do something like markdown? That's not going to work...
You could try this:
https://github.com/ziggi/zimg-host
You can convert an image to base64 encode and then play with it.
Example:
$img = file_get_contents('<path>/image.jpg');
$data = base64_encode($img);
As result, you will have a bit string that you can save the value in the session.
But keep in mind that this is not a best practice.
Also, you can use this string in the source of an image.
Example:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAyACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" />
Related
How should I put an image into <input type="file"> where the image is from my database(actually, the image name only written into database, and the image store in my folder), just like a value="" in the other input type, but <input type="file" value="<?php echo $imagefileerwerewr; ?>"> doesn't work to retain the image file.
There is not a way that you can just put you image inside an file input and you can update image. I would suggest you that you use both img tag and input tag to archieve this. You can use img tag to show the current image and also you can use input to let the users to update another image if they want.
I would suggest you to use dropzone it has some great features and would be easier for you to manage all this
View
<form class="m-form" action="<?php echo AURL;?>products/update_product/<?php echo $products['product_id'];?>" method="post" enctype="multipart/form-data">
<input type="file" name="product_image_name" class="form-control m-input dropify" placeholder="" data-default-file="<?php echo Website_Assets.'images/'.$products['product_image_name'];?>" value="<?php $products['product_image_name'];?>" data-max-file-size="2M" required>
</form>
In above code product_image_name not taking any value but showing the image picking the path and when I change image it post the image_name
Controller
public function update_product($product_id)
{
echo "<pre>";
print_r ($_FILES['product_image_name']);
echo "</pre>";
exit();
}
changing the image works ok but if i donot change image its not picking the default value of the image
Short answer: An input of type file can not have a default value.
Instead use an <img />-tag to show default images. For example:
<img src=" <?php echo Website_Assets.'images/'.$products['product_image_name'];?>" />
Normal answer: Assuming you are trying to upload an image, save it to the entity that is behind the form and later edit it; try to think around using just the one field. Instead perhaps a sequence like this:
Upload a file using the upload field
Save the uploaded file on the machine that runs PHP (server-side)
Store the path to the uploaded file in the entity
In the edit view of the entity show both:
the image that was uploaded (or a placeholder)
an upload field to upload a new image
Good luck!
Tip: You might also want to have a look at open source libraries like dropzonejs. Existing libraries often give examples and excellent documentation. This example visually combines the upload field with the display field.
I've looked at this PHP file upload/resize method, but I have two questions.
When the image file is uploaded to the directory, the file is the name of the image uploaded from the person's device. I'd prefer it to be some kind of ID / number so that, for example, if person A uploads an image from their device called image1.jpg and so too does another person, it won't overwrite the original.
I know that imgur uses a 7-character string with numbers 0-9 and letters a-Z, so maybe it could generate something similar but also check if it already exists, and if it does, generate another. I'm not sure how to achieve this kind of thing.
I also want to insert the image that's been uploaded into a table.
How could I achieve these two things using the method I linked?
<?php
if(isset($_POST['submit']))
{
$newname = time().'_'.$_FILES['image']['name'];/*Rename image - New unique name of image*/
move_uploaded_file($_FILES['image']['tmp_name'],'upload/'.$newname);/*upload image*/
/*Now use this $newname and use it for insert into database*/
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>
We are trying to create a site for wallpapers so that would be somehow large files(2mb-5mb) so we'll be needing to store the images on the disk space instead in the database and only the paths to the database. So if you can give some ideas on how to do that (the method we know for now is creating a PHP script with the upload function and by manually selecting the images from the PC to be uploaded) unless you guys would have other suggestions. Tutorials will be much appreciated. Thanks a lot!
This is for the admins to add images not for the users.
Note: we haven't developed any script so this is to get some ideas from you guys on what we can use with this, if none guess we will just go with the php script.
Your form,
<form action="PHP_FILE_PATH.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Upload" />
</form>
The PHP Part
<?php
if($_FILES['image']['name'])
{
$save_path="FOLDER_PATH_TO_SAVE_UPLOADED_IMAGE"; // Folder where you wanna move the file.
$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
}
$inser_into_db="INSERT INTO `database`.`table` (`folder_name`, `file_name`) VALUES('$save_path', '$myname'))";
?>
For each file uploaded, generate a UUID and use that for the filename on disk. That avoids collisions, sanitizing filenames and path traversal vulnerabilities.
You'll have a table like this: (id, description, filename) with values like (1, "Green field", "0D729DCD-5116-4480-81CE-90A0380B557A.png").
Next, you want to avoid the problem of having too many files in one folder — you'll hit a filesystem limitation for many FSes.
To work around this problem, create directories based on the first few letters of the filename. For 0D729DCD-5116-4480-81CE-90A0380B557A.png, you would store it in /0/D/7/0D729DCD-5116-4480-81CE-90A0380B557A.png.
I'm trying to insert an image into my database. I've read a few posts and I'm clearly doing this wrong. I know that the column datatype needs to be a blob for the image. This I have done.
My form looks like this:
<form id="Dev_test" name="Dev_test" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP']);?>" enctype="multipart/form-data">
<input type="file" name="call_file" id="call_file">
<input type="submit" name="submit" id="submit" value="SUBMIT">
</form>
This is where I've seen multiple variations of how to do this, and I even came across a post that said this might not be possible.
$query = "INSERT INTO `******`.`******` (img) values ('{$_POST['file']}')"
I know the above isn't right.
So my question(s) is/are the following, is there a reason why $_POST['file'] isn't posting, and is there a better method to insert the image into my database? As an fyi, I'm aware that inserting an image directly into the database is not the most efficient method and that there are other methods by referencing file paths.
What you want to use is php's $_FILES superglobal instead.
$content = file_get_contents($_FILES['call_file']['tmp_name']);
You may want to do virus checking on $content or the like.
I would also suggest that you store the file on the system rather than as a blob in the database and store the path to the file instead.
To store image file in a DB (which is generally a bad idea because filesystem is an db designed specially for effective storing of files) you need to read the contents of the uploaded file and then put it into the query.
In php you should use $_FILES global array to get information about uploaded files, use is_uploaded_file() to check if everything is ok with uploaded file, then you have to read the contents of the uploaded file with standard file access functions and then you MUST escape the contents of the file when inserting it to database.