php - Submitting Image From Form Apparently Wrong Octet
Get the solution ↓↓↓A simple form with an image viewer below it in the same test.php script, the image gives errors in displaying. Disabling the headers and viewing the image data on the screen, it seems to have many of the little black ? diamonds all over it so probably somehow the wrong octet is being submitted. Where does the encoding get lost and how do I fix it?
To clarify, I am not trying to upload and save the file anywhere and I am not trying to insert it into a database. I simply want the data stream to directly display itself on the screen.
Also, the conditional around the PHP code is simply so that it does nothing until it receives a file. No validation needed as this is used only locally on my development system as a test tool.
test.php
<form method="POST" action="test.php" enctype="multipart/form-data">
<fieldset>
<legend>Image Functions Tester</legend>
<p><label for="image">Image</label>
<input type="file" name="fileToProcess" id="fileToProcess">
<p><input name="insert" type="submit" value="Upload">
</fieldset>
</form>
<?php
if (isset($_FILES['fileToProcess'])) :
$file = $_FILES['fileToProcess'];
$image = file_get_contents($file['tmp_name']);
// TEST ConvertImage() FUNCTON HERE
// Get the image mime type
$mimetype = Array("1" => "image/gif",
"2" => "image/jpeg",
"3" => "image/png",
"6" => "image/bmp");
// Add headers and output the image
Header("Pragma: no-cache");
Header('Content-type: ' . $mimetype[exif_imagetype($image)]);
echo $image;
endif;
?>
Answer
Solution:
I guess you are trying to display uploaded image. in your code I see that you are trying to validate to ensure that user submit only images. In php they are just two best method to ensure that submitted files are not virus but image
1.) using Getimagesize Function
2.) Fileinfo method.
I have implemented the validation in your code to ensure workability.
First create a directory call uploads where files will be uploaded and run this your update code
<form method="POST" action="test.php" enctype="multipart/form-data">
<fieldset>
<legend>Image Functions Tester</legend>
<p><label for="image">Image</label>
<input type="file" name="fileToProcess" id="fileToProcess">
<p><div class="ButtonCenter">
<input name="insert" type="submit" value="Upload">
</div>
</fieldset>
</form>
test.php
<?php
if (isset($_FILES['fileToProcess'])) {
$uploadDir = "uploads/";
$filename= $_FILES['fileToProcess']['name'];
$file_types=array(
'image/gif',
'image/jpeg',
'image/png',
'image/jpg',
'image/GIF',
'image/JPEG',
'image/PNG',
'image/JPG'
);
// validate image using getimagesize function
$image_info =getimagesize($_FILES['fileToProcess']['tmp_name']);
//print_r($image_info);
$mime_image = $image_info['mime'];
// ensure the file is image via getimagesize function
if ( ! ( in_array($mime_image, $file_types) ) ) {
echo "you can only upload images";
exit();
}
//validate image using file info function
$f = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($f, $_FILES['fileToProcess']['tmp_name']);
if ( ! ( in_array($mime, $file_types) ) ) {
echo "You can only upload Images";
exit();
}
finfo_close($f);
if(move_uploaded_file($_FILES['fileToProcess']['tmp_name'],"$uploadDir/".$filename)) {
echo "File uploaded successfully";
echo "<img src='uploads/$filename' width='100px' height='100px'>";
}
}
?>
Answer
Solution:
After fighting with this for two days, I just found the problem although I don't fully understand the reason it makes a difference. Apparently the form and the viewer cannot be on the same page so splitting them into two files works perfectly:
test.php
<?php
if (!isset($_FILES['fileToProcess'])) : ?>
<div class="PageCaption">Site Administration<hr></div>
<form method="POST" action="testviewer.php" enctype="multipart/form-data">
<fieldset>
<legend>Image Functions Tester</legend>
<p><label for="image">Image</label>
<input type="file" name="fileToProcess" id="fileToProcess">
<p><div class="ButtonCenter">
<input name="insert" type="submit" value="Upload">
</div>
</fieldset>
</form><?php
endif; ?>
testviewer.php
<?php
if (isset($_FILES['fileToProcess'])) :
$file = $_FILES['fileToProcess'];
$image = file_get_contents($file['tmp_name']);
// Get the image mime type
$mimetype = Array("1" => "image/gif",
"2" => "image/jpeg",
"3" => "image/png",
"6" => "image/bmp");
// Add headers and output the image
Header("Pragma: no-cache");
Header('Content-type: ' . $mimetype[exif_imagetype($image)]);
echo $image;
endif;?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: deprecated: directive 'allow_url_include' is deprecated in unknown on line 0
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.