I've seen tutorials over the internet for this, but I cannot get it to work.
I've got this in a while loop, but it shouldn't matter:
while($row = mysql_fetch_object($result)){
$image_url = $deviceimg.($row->Image);
if(!file_exists($image_url))
{}
else {
$image_url = 'images/android.png';
}
Basically, im my database, all the rows called Image are populated, but only some of them have an actual file. What im trying to do is check to see if the image file actually exists on the server, if it doesn't use a default image (images/android.png)
No matter that I do, it will either replace ALL images with the default, or it will just use the URL in the database, regardless of if the file actually exists.
The reason you have a problem is because you have wrapped $image_url in single quotes when you pass it to file_exists(). This is checking whether there is a file file literally named $image_url, instead of using the contents of the variable.
Change
if (!file_exists('$image_url'))
to
if (!file_exists($image_url))
and it will work.
'$image_url' does not get parsed to the contents of the variable $image_url: it will stay the same string literal.
Remove the quotes. Only variables in double quotes ("$image_url") will get parsed.
Related
I am building a web application using CodeIgniter framework. The issue is that while uploading a file that have a single quote ' in the file name (Eg : Rob's.wmv) I get an empty file array in the controller. CI do have code to update the file name when the name contains special characters, but since the array is not set correctly the uploading doesn't take place. I can't understand what's going on, I tried debugging, it appears the uploading works fine on my local machine, but not on the server, this makes it even more interesting.
UPDATE The file array var_dump returns an empty array, so there's no way I can even get the file name in the script. The file array is not set, I don't get any information about the file at all.
You have to use the addslashes() from php.
The addslashes() function returns a string with backslashes in front of predefined characters.
example:
$var = "Rob's.wmv";
//then
$var = addslashes($var);
The predefined characters are:
single quote (')
double quote (")
backslash (\)
NULL
Tip: This function can be used to prepare a string for storage in a database and database queries.
As you upgrade your question:
please read this two article carefully, this is just a configuration problem. After that i think you are able to do it.
codeigniter-file-upload
how-to-upload-file-in-codeigniter
I'm writing a script (in PHP) which will go through a PHP file, find all instances of a function, replace the function name with another name, and manipulate the parameters. I'm using get_file_contents() then strpos() to find the positions of the function, but I'm trying to find a good way to extract the parameters once I know the position of the start of the function. Right now I'm just using loop which walks through the next characters in the file string and counts the number of opening and closing parentheses. Once it closes the function parameters it quits and passes back the string of parameters. Unfortunately, runs into trouble with quotes enclosing parentheses (i.e. function_name(')', 3)). I could just count quotes too, but then I have to deal with escaped quotes, different types of quotes, etc.
Is there a good way to, knowing the start of the function, to grab the string of parameters reliably? Thank you much!
EDIT:
In case i didn't read the question carefully, if you want to only get function parameters,you can see these example :
$content_file = 'function func_name($param_1=\'\',$param_2=\'\'){';
preg_match('/function func_name\((.*)\{/',$content_file,$match_case);
print_r($match_case);
but if you want to manipulate the function, read below.
How about these :
read file using file_get_contents();
use preg_match_all(); to get all function inside that file.
please not that i write /*[new_function]*/ inside that file to identify EOF.
I use this to dynamically add/ delete function without have to open that php files.
Practically, it should be like this :
//I use codeigniter read_file(); function to read the file.
//$content_file = read_file('path_to/some_php_file.php');
//i dont know whether these line below will work.
$content_file = file_get_content('path_to/some_php_file.php');
//get all function inside php file.
preg_match_all('/function (.*)\(/',$content_file,$match_case);
//
//function name u want to get
$search_f_name = 'some_function_name';
//
for($i=0;$i<count($match_case[1]);$i++){
if(trim($match_case[1][$i]) == $search_f_name){
break;
}
}
//get end position by using next function start position
if($i!=count($match_case[1])-1){
$next_function= $match_case[1][$i+1];
$get_end_pos = strripos($content_file,'function '.$next_function);
} else {
//Please not that i write /*[new_function]*/ at the end of my php file
//before php closing tag ( ?> ) to identify EOF.
$get_end_pos = strripos($content_file,'/*[new_function]*/');
}
//get start position
$get_pos = strripos($content_file,'function '.$search_f_name);
//get function string
$func_string = substr($content_file,$get_pos,$get_end_pos-$get_pos);
you can do echo $func_string; to know whether these code is running well or not.
Use a real parser, like this one:
https://github.com/nikic/PHP-Parser
Using this library, you can manipulate the source code as a tree of "node" objects, rather than as a string, and write it back out.
Not sure if the title for this is correct but here's my problem:
I have a table that hold image's url like so:
img/folder/imagename.jpg
Now I've created a thumbnail for each image in each folder, so if I want to display them I do a loop in my table and return all the url's but I need to add the word "thumbs" after "folder/" and before the "imagename.jpg"...Now because obviously the "folder" and "imagename" names differ in length then I can't do a count..the only thing I can figure is to look up that last "/" character and insert there and add on the "imagename.jpg" after it so end result would look like:
img/folder/thumbs/imagename.jpg
Just replace the path with a "deeper" path:
$path = str_replace('img/folder/', 'img/folder/thumbs/', $path);
There are lots of other ways to do this, but IMHO this one is perfectly clear on what it does. Theoretically it won't work universally (if your path contains multiple occurreces of img/folder/ for some reason), but let's just not go there.
you can also
$pathparts = explode("/",$path);
and then use $pathparts array to construct your path again. You would use it in cases you want to have more control over manipulating paths, but in heavy loads it's not very efficient.
echo $pathparts[0]."/".$pathparts[1]."/thumbs/".$pathparts[2];
//adition
And why not to update your database and script only save imagename.jpg
and then in the beginning of calling script define
define("IMGPATH", "img/folder/");
define("THUMBPATH", "img/folder/thumbs/");
and then call it
<?= IMGPATH."imagename.jpg" ?>
<?= THUMBPATH."imagename.jpg" ?>
I have a php file that takes an image from a website and stores it in my database. The name of the file contains an equals symbol. The file I end up storing is the image I get when I change the equals sign into '%3' in the URL. How can I make it pick up the file that is in the location whose URL maintains the '='?
$idNum=$_GET['idNum'];
$testpage = file_get_contents('http://www.something.com/php/thePic.php?id=$idNum');
$testpage = mysql_real_escape_string($testpage);
mysql_query("UPDATE tblSomething SET Pic = '$testpage' WHERE id='$idNum'");
Thanks,
R
The filename is coming out in a "urlencoded" form.. You need to use the urldecode() function to get back the = sign.
urldecode($string);
See more here - http://php.net/manual/en/function.urldecode.php
So before your SQL query, add this line-
urldecode($field);
And yes.. You are vulnerable to SQL injection attacks..
With these attacks, your WHERE clause can be easily bypassed. Just make sure you dont do any sensitive processing with this kind of a query.
Hello every one i m using fpdf libray to creat pdf files from html form.
i m using
$pdf->Image('C:/DOCUME%7E1/mypic.PNG',60,140,120,0,'','');
to display image on pdf.
in its first parameter it asks for exact path.it doesn't accept anyaddress variable here.
but i want to make dynamic.i have able to get a complete path in an variable.
i have printed this variable.
//////////////////////////////
echo "$path";
output
////////////////////
C:/DOCUME%7E1/mypic.PNG
/////////////////////////////////////////////////////
but how i put that path from variable in this parameter.?
when i use this variable as in this function.it give error.
$pdf->Image('$path',60,140,120,0,'','');
plz help me for this.
$pdf->Image($path,60,140,120,0,'','');
This should work.
I removed the single quotes from the variable.
OR
put the $path in double quotes:-
$pdf->Image("$path",60,140,120,0,'','');
Remove the single quotes wrapping the variable name.
The single quotes make PHP treat your string as a literal (i.e. $path instead of C:/DOCUME%7E1/mypic.PNG).
Confusingly, it would work with double quotes because of variable interpolation.