PHP get image size - php

I'm using the following code but it gives me an error about an unexpeted " in the statement at this line, but I'm not sure how the syntax should go:
list($width,$height,$type,$attr) = getimagesize("' . $SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo'] . '");
echo "<p>This logo is ".$width; x $.height; echo "pixels in size.</p>";
PHP says the error is on that first line.

Try this
list($width,$height,$type,$attr) = getimagesize($SESSION_['html_folder'] . '/uploadedfiles/' . $row['logo']);
The error in your code was that you opened the string "twice", one with " and one with '. If you use a variable as parameter where a string is expected, you do not need to set the variable in quotes.
You should make sure that in $SESSION_['html_folder'] there's no malicious code, e.g. with
if(!is_dir($SESSION_['html_folder']))
die("ERROR");

youre adding all kinds of unnecessary quotes
list($width,$height,$type,$attr) = getimagesize($SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo']);

You want:
list($width,$height,$type,$attr) = getimagesize($_SESSION["html_folder"] . '/uploadedfiles/' . $row['logo']);
The main issue was that you were using double quotes to open a string, but you didn't close the string with double quotes. The above is a better formated string(and fixed your $_SESSION variable).

Related

PHP Line break in array

I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:
Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?
Thanks a lot for any help!
$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];
$data = array($name, . "\n" $mail, . "\n" $saywords);
file_put_contents("$t.$ip.txt",$data); // Will put the text to file
You have two problems:
$data should be a string not an array
. concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.
Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.
i don't see the use of array , you can concatenate them just like :
$data = $name . "\n" . $mail . "\n" . $saywords ;
It depends with the operating system of the server.
Try "\r\n" instead of "\n"
Okay, so here is my shot.
/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0.
In such way you will avoid PHP warnings, when some of these variables
will not be set*/
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
$mail = !empty($_POST['mail']) ? $_POST['mail'] : '';
/*Secondly, do not use \n, \r, \r\n, because these are platform specific.
Use PHP_EOL constant, it will do the job perfectly, by choosing
what type of line-break to use best.
As others mentioned - in your scenario, the string would be better solution.
Add everything into string, and then put its contents into file. Avoid using
double quotes, when you define PHP strings, and use single quotes instead - for
performance and cleaner code.
*/
$data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;
file_put_contents($t.$ip.'.txt', $data); // Will put the text to file
By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.
Tips:
1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)
2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...
3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.
You will get much cleaner submissions by doing this way.
Use <br /> as you use html code

redirecting to a URL query string

I am trying to redirect a page to:
product_page.php?rest_id=$rest_id&area=$rest_city
for example:
product_page.php?rest_id=3&area=Enfield
At the moment the page is redirecting to:
http://localhost/PhpProject2/product_page.php?rest_id=&area=
Even though i have specified :
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
(int)$_GET['add_item']]){
$_SESSION['Shopping_cart_' . (int) $_GET['add_item']]+='1';
//redirect
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>"; //header does not work
}
How would i go about getting the echo to re-direct to the correct page.
What i have tried:
Calling both add_item, rest_id and rest_city in the same if asset.
Moving the $_GET rest_id and rest_city closing tag after the echo.
The two above options just stop the page from re-directing all together, this is my first time doing this, any suggestions or tips would be greatly appreciated.
You should use double qotes ["string $variable string"] for variable to work inside a string or if not you should concatenate the string with [.] sign.
echo"<script>window.open('product_page.php?rest_id=$rest_id&area=$rest_city','_self')</script>";
This will not work because of single qoute [']
Regards
There are many syntax errors in your code. Also, the way you echo the string is wrong. You need to concat the variables with the string, using the PHP concatenation operator, else use double quotes.
Below fixed your errors:
if (isset($_GET['rest_id'])){
$rest_id = mysqli_real_escape_string($dbc,$_GET['rest_id']);
}
if (isset($_GET['area'])){
$area = mysqli_real_escape_string($dbc,$_GET['area']);
}
if (isset($_GET['add_item'])) {
$_SESSION['Shopping_cart_'] = (int) $_GET['add_item']+='1';
echo"<script>window.open('product_page.php?rest_id=" . $rest_id . "&area=" . $rest_city . "','_self')</script>";
}

PHP - Echo out database information into a string

I am simply trying to echo or print out specific data from a DB into a string (i hope thats the right name), which should be a very simple process as I've done it before. The point is everytime a user inserts information into the database this string echo's or prints out the inserted data.
But for some very odd reason this time around when i try to echo out the data, I literally get this.
Very frustrating. As you can see from the image above i have tried using 2 different ways to do this a variable and a session, but the echo literally just prints it out. I have done this before so i am aware that it is possible. I am just a little lost into how i am meant to achieve this or even better where i went wrong. I know how to do this using a different style of coding, but i am trying to keep everything uniformed (newbie).
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo '
<p>$addon . " " . $_SESSION["Add_On_Price"]; </p>
';
My session is started and the php file is connected to the DB.
I also have error handling which has not given out any error messages.
You must do:
echo "<p>$addon ".$_SESSION["Add_On_Price"]."; </p>";
A string encapsulated into ' is rendered just as it is.
Use " to render a string that contains variables. Example:
$a = 3;
$a++;
echo "the result is $a";
will result in the result is 4.
On the other hand,
echo 'the result is $a';
gives the result is $a.
As the documentation points out:
Single quoted ¶ The simplest way to specify a string is to enclose it
in single quotes (the character ').
Doued ¶
If the string is enclosed in double-quotes ("), PHP will interpret
more escape sequences for special characters
Try not mix it..
And if within double quotes you have an associative array you may concat.
echo "string $variable". $array["index"];
or
echo "string $variable {$array["index"]}";
Then your code should look like
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo "<p>$addon {$_SESSION["Add_On_Price"]}; </p>'";
}
Long time ago
I never use double quotes due to it require parse the whole string for special notations. However it.
Try not mix single quotes with double quotes. pick up a standard for you code you will not notice any difference than is easy to code and read without surprises
You're mixing single quotes and double quotes. Single quotes do not perform interpolation of variables so when you write this:
echo '... whatever including " char and $ sign';
PHP will just literally print everything inside.
You forget some ' or " !
echo '<p>' . $addon . ' ' . $_SESSION["Add_On_Price"] . '</p>';
Use double quotes
echo "<p>$addon $_SESSION['Add_On_Price']; </p>";

PHP: Complicated String With Single and Double Quotes

I'm trying to pass GET variables inside the URL with a bit of html inside of my PHP but can't figure out the quotes situation. I need to embed two variables inside the URL. I have one in but don't know how to embed the other. Here is the string:
echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>";
and here is what I need to go inside
&City=$City
Thanks for the help
Its pretty simple,
echo "<a href='?id=".($id-1)."&city=" . $City . "' class='button'>PREVIOUS</a>";
In php double quotes "" can eval variables inside them.
$test = "123;"
echo "0$test456"; // prints 0123456
In your case you better use single quote ''.
echo '<a href=\'?id=' . ($id-1) . '&City=' . $City . '\' class=\'button\'>PREVIOUS</a>';
or better
echo 'PREVIOUS';
Use something like this:
echo "<a href='?id=".$id."&City=".$city."'>";
You do need (well, it's good practice anyway) to use & for your ampersand. Otherwise it's fairly straight forward;
echo "<a href='?id=".($id-1)."&City=$City' class='button'>PREVIOUS</a>";
This is because you are using double quotes, which means you can put variables directly into the string (there are some exceptions which you might need to put in curly brackets {}).
I suggest you get a text editor with syntax highlighting, such as jEdit (other editors are available).
Hope this helps.
Maybe is it better to use the sprintf function
$id = 100;
$previousId = $id - 1;
$City = 'Amsterdam';
$link = 'PREVIOUS';
echo sprintf($link, $id, $City);

How do I print a character variable within quotes using php function

I need to get this output.
the result is"random"safdsaf
I am using this piece of code
<?php
$x = "random";
echo 'the result is' .$x. 'safdsaf';
?>
But i am getting this
the result israndomsafdsaf
I have to define random before printing it.
i.e. I do not want to change this piece of code
<?php
$x = "random";
What change should i make inside echo to get the desired output?
If you are using the same type of quotes delimit the quotes in your string like this:
echo "The result is\"" .$x. "\"safdsaf";
or simply use two sets of different quotes:
echo 'the result is"' .$x. '"safdsaf';
Output of either line of code:
The result is"random"safdsaf
Try this
Added the little bit space befor ' and added ", it will give the some out put as you want
echo 'the result is "' .$x. '" safdsaf';
the result will be
The result is "random" safdsaf
If you want to print out double quotes you can include them in single quotes
Something like this would do the trick.
$x = '"random"';
If for whatever reason you don't want to use single quotes you can also escape them like :
$x = "\"random\"";
As you want to keep the string, I suggest you change the original line where you put it in :
echo 'the result is"' .$x. '"safdsaf';
the principle stays the same
Here's some reading material : http://php.net/manual/en/language.types.string.php
You can use simply :-
echo 'the result is "' .$x. '" safdsaf';
OR you can use .
echo "the result is \" $x\" safdsaf";
Using the \ before the quote like this: the result is \"random\" safdsaf.
if you have alot of quotes and such in a string, i would suggest using the addslashes(). This method will do the work for you for you.
For more info, take a look here - http://www.w3schools.com/php/func_string_addslashes.asp

Categories