php string comparison not working for image metadata - php

I am extracting image metadata using php. The logic of my below code is that if the user uploads the default file with metadata(UserComment)=ASCIIsd11, he/she will get an error.
<?php
$exif_s = exif_read_data('e42889ed00.jpg');
$phtchk = $exif_s["UserComment"];
print $phtchk;
print strcmp($phtchk, "ASCIIsd11");
if(strcmp($phtchk, "ASCIIsd11") == 0){ echo "You have not uploaded your own photo"; exit;}
else
{
echo"You have uploaded it.";
}
?>
print $phtchk; returns ASCIIsd11
print strcmp($phtchk, "ASCIIsd11"); returns -1
and the last echo statement "You have uploaded it" is printed. Actually I am expecting strcmp() to return 0. Kindly help.

Do var_dump(phtchk); instead of print $phtchk;
Perhaps you don't see some extra-chars (eg: \n).
If it concerns the collation, you should see:
UTF-8 characters not displaying properly from JPEG IPTC data in PHP

Related

checking value of if conditino isn't printing correct output

I am working on a chat room and the condition i want to implement is:
when user send both file and text message it will return an error.
but this isn't working correct.
this is my code
if($_FILES['chat_upload_file']){
echo "You are in file checking<br/>";
echo $text_message;
if($text_messege){
echo "It's in IF<br/>";
$error = 1;
$error_msg = "Either Sender can send file or text";
exit(0);
}
else{
echo "it's in else<br/>";
exit(0);
}
here $text_messege = "hello user";
the output that i want is from internal IF : It's in IF
but the output is : It's in else
try
if($text_messege == "hello user"){
this will evaluate that $text_message is set to "hello user"
if($text_messege){
merely evaluates that $text_message is set and is set to something other than null or false.
Try this:
if(!empty($_FILES['chat_upload_file']) && !empty($text_messege)){
//both
}else if(!empty($_FILES['chat_upload_file'])){
//file only
}else if(!empty($text_messege)){
//message only
}else{
//nither
}
I would probably use empty instead of just testing if it's true or false. Empty will cover undefined index issues you may have. Without more to go on, I have no way to know if that is a possibility. For example I have no Idea where $message comes from and if it's always considered "set" (or not undefined).
You can optimize this by doing something like this.
$file = !empty($_FILES['chat_upload_file']) ? $_FILES['chat_upload_file'] : false;
$text_messege = !empty($text_messege) ? $text_messege : false;
if($file && $text_messege){
//both
}else if($file){
//file only
}else if($text_messege){
//message only
}else{
//nither
}
So you only check empty 2x instead of 4x.
Original and Issues
the condition i want to implement is: when user send both file and text message it will return an error.
You have to (or should) consider 4 possibility (2^2) each variable can have 2 states, well call them on and off
File | Message
on on
on off
off on
off off
In your original, you are only considering 2 of them
File | Message
on on
on off
Because:
if($_FILES['chat_upload_file']){
if($text_messege){
//file: on message: on
}else{
//file: on message: off
}
}
Does not consider what happens when no file is submitted. And if it's an error to send a File and a Message, then it must be ok to send just a message and no file. Otherwise, you would never be able to send a message (you cant send on with a file, and you cant send one without). Maybe there is stuff below this? I have no way to know that, though.
It makes little sense to add an else to the outer IF, because you will probably wind up duplicating stuff.
if($_FILES['chat_upload_file']){
if($text_messege){
//file: on message: on
}else{
//file: on message: off
}
}else{
if($text_messege){
//file: off message: on
}else{
//file: off message: off
}
}
And it's a lot cleaner to have 1 control block with 4 conditions, then 3 control blocks with 6 conditions.
It's implied by what you want to do that:
it's fine to have just a file
it's fine to have just a message (you don't consider it)
it's not fine to have both a file and a message
who knows with neither (you don't consider it)
hope that makes sense.

pdf and word file uploading in php

In here i have tried to upload jpeg,pdf ,png and word file formats in php. But i could not able to upload pdf,png and word file formats.but i have successfully upload the jpeg file formats. So this is the code i have tried.please help to edit this code to upload other file formats with out the jpeg file format.
<?php
include_once("db.php");
if(isset($_POST['save'])){
if(($_FILES['file']['type']=='pdf/pdf')
||($_FILES['file']['type']=='image/jpeg')
||($_FILES['file']['type']=='image/png')
&&($_FILES['file']['size']<200000))
{
if($_FILES['file']['error']>0)
{
echo"return code :".$_FILES['file']['error'];
}
//else if(file_exists('upload/'.$_FILES['file']['name']))
//{
//echo $_FILES['file'] ['name']."Already exite";
//}
else if(move_uploaded_file($_FILES['file'] ['tmp_name'],'upload/'.$_FILES['file']['name']))
{
$part =$_FILES['file']['name'];
$sql = mysql_query("INSERT INTO stu (ptype,source,letterno,title,descrip,receiver,image)
VALUES ('{$_POST['pt']}',
'{$_POST['so']}',
'{$_POST['ln']}',
'{$_POST['lti']}',
'{$_POST['dic']}',
'{$_POST['re']}',
'{$part}')");
//$sql= "INSERT INTO stu (ptype,source, letterno, title,descrip,receiver,image) VALUES ('$p', '$s', '$l', '$t','$d','$r','$part')";
if ($sql){
echo"jhgjhgjh";
//echo "successfully insert thise record";
//echo "<script type='text/javascript'>alert('successfully insert thise record')</script>";
echo "<script type='text/javascript'>alert('successfully insert thise record')</script>";
}
}
}
}
?>
PDF documents have different MIME type: application/pdf, you should use it.
Microsoft Word documents MIME type application/msword is not in list of your allowed mime types, so it is failed to get through your check.
You have size limit of 200kib, it is about 195kb, probably you have documents larger then this (pretty low) limit.
After raising local file limit - check your upload_max_filesize setting in php.ini since it is next size limitation boundary, you may stuck on.
Besides this your code have logical problem into MIME type checking condition since you didn't group MIME checks into separate logical value. Your condition is actually: if (a OR b OR c AND d) while is should be if ((a OR b OR c) AND d).
It will be even better to rewrite this code as:
$allowed_mime_types = array('image/png','image/jpeg','application/pdf','application/msword');
$size_limit = 200000;
if ((in_array($_FILES['file']['type'], $allowed_mime_types)) && ($_FILES['file']['size'] < $size_limit)) { ... }
try changing :
$_FILES['file']['type']=='pdf/pdf'
to:
$_FILES['file']['type']=='application/pdf'
for doc type:
$_FILES['file']['type']=='application/msword'
for docx:
$_FILES['file']['type']=='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Note:
also check if the file size of the file that you're trying to upload is not greater than allowed.

Count the number of files selected for upload within multiple file fields

I've wrote a script where the user can select to upload more than one image to a form. I'm preventing the user from submitting the form until all of the file fields that have been added actually contain files for upload.
EG:
The user has three file fields to fill in, therefore three files must be ready for submission to the database.
I'm using the same id on each of the file upload fields. When I use print_r($_FILES); it returns me an array. If I browse for a file in the first file field, and leave another two blank, it will state that the array object for [1] and [2] are blank, however [0] will obviously have a name, type, size etc as it exists.
How would I go about making sure that all of the file fields are actually filled in using PHP?
Thanks in advance, Rich
Here are my efforts so far:
$imgName1 = $_FILES['upload1']['name'];
$imgTmp1 = $_FILES['upload1']['tmp_name'];
$imgType1 = $_FILES['upload1']['type'];
$imgSize1 = $_FILES['upload1']['size'];
print_r($_FILES);
if(!$imgTmp1){
echo "<span class='error'>You need to include at least one image with this article.</span>";
exit();
} else {
$fileCount=($_FILES['upload1']['name']); // My attempt to count that the file fields are all filled in
$cnt = $_POST['cnt']; // This is the number of file fields that currently exist
echo "<br/>";
echo count($fileCount);
if($cnt != $fileCount){
echo "<span class='error'>You have not uploaded all of your files.</span>";
exit();
}
// etc etc
You have incorrect upload checking. The presence something in $_FILES doesn't mean that file uploaded. A failed upload will STILL create $_FILES entries.
You need to check for BOTH the presence of the $_FILES entry, and its error parameter:
if (isset($_FILES['upload1'])) {
if ($_FILES['upload']['error'] === UPLOAD_ERR_OK) {
... file was successfully uploaded
} else {
... upload failed
}
} else {
... no file upload was even attempted
}

Php Check if 2 from Database are empty

Hello I am working on simple video script where I need to do the following:
I need to check if Variable Source is empty (if it's not empty echo $source, but if it is empty proceed to check other variable below)
if Variable Embed is set, if it is set (echo $embed), but if it is empty do
echo $image
Basically I have setup system for Videos that can load either from Youtube Video with iframe, either with Embed from all kinds of other pages
but if Both of this fields are empty I need to show just image that will be set from URL
You can use an elseif-statement.
if(isset($source) && $source) {
echo $source;
} elseif(isset($embed) && $embed) {
echo $embed;
} else {
echo $image;
}

php put_file_contents location problems, duplicate files

The code below fetches a jpg image from a server. if the length is larger than 1, we assume to have successfully grabbed the file. Then it proceeds to create a folder for the user if it isnt already there. (this part works)
from there, it should save the file in the /media/downloads/username location
instead it is going to /media/downloads/
I've looked at my concatenation a few times now, but I don't see where the issue is. I SEE that the filename is coming out with the username attached to the front, so I imagine its just a concatenation error.
Once this is fixed, I have a bigger issue- because I am using rand, when I cycle the script it just gives me duplicates. how can I prevent the duplicate images?!
if (strlen($data) > 1) {
$prePath = 'media/download/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.'.jpg';
if (!file_exists('media/download/'.$username)) {
mkdir('media/download/'.$username, 0777, true);
}
if (file_exists($prePath)){
$finalPath = 'media/download/'.$username.'/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.rand(0,100).'.jpg';
}
else {
$finalPath = 'media/download/'.$username.'/from_'.$sender.'_to_'.$recipient.'_id_'.$snapid.rand(0,100).'.jpg';
}
//echo "<img src='$data'></img>";
file_put_contents($finalPath, $data);
echo " <img src='".$finalPath."' alt='Smiley face' height='100' width='100'> ";
}

Categories