I have this code, which will create result.png on my server:
imagepng($image, "'./result/result.png");
Now I have $text , $type , $id
How can I create file with result_$text _ $id _ $type?
Example: ./result/result_123_131_1.png ;
./result/result_Text_1364_3.png
It's very simple:
$name = "./result/result_$text_$id_$type";
//do whatever you do to save the image and use $name as name of the file.
In php the double quotes are used whenever you have variables you want to use in some string literal. What I do above is I insert the value of the variables into the string literal "./result/result___".
More info here.
$image = imagepng($image, "'./result/result");
$file = $image . "_" . $text . "_" . $id . "_" . $type ."png";
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
I've been working on a php dynamic drop down list that scans a folder on the web server and list its contents it works hears relevant selection of the code.
<?php
foreach(glob(dirname(rootdir) . '/path/username/*') as $filename){
$filename = basename($filename);
echo "<option value='filepath/username/" . $filename . "'>".$filename."</option>";
}
?>
However if i use a variable to populate part of the file path the variable doesn't get added to the path.
<?php
$name = "jsmith";
foreach(glob(dirname(rootdir) . '/path/$name/*') as $filename){
$filename = basename($filename);
echo "<option value='filepath/$name/" . $filename . "'>".$filename."</option>";
}
?>
$name = "jsmith"
The result I'm looking for is /path/$name/* = /path/jsmith/
and
filepath/$name/ = filepath/jsmith/
How do I get the glob(dirname) and option value=' recognize the variables?
You need to either concatenate the variable with the string or wrap the string in double quotes so the variable is parsed.
Double Quotes
<?php
$name = "jsmith";
foreach(glob(dirname(rootdir) . "/path/$name/*") as $filename){
$filename = basename($filename);
echo "<option value='filepath/$name/" . $filename . "'>".$filename."</option>";
}
?>
Concatenate
<?php
$name = "jsmith";
foreach(glob(dirname(rootdir) . '/path/' . $name . '/*') as $filename){
$filename = basename($filename);
echo "<option value='filepath/$name/" . $filename . "'>".$filename."</option>";
}
?>
Single quotes do not interpolate. (i.e. Variables in the string will not be replaced with their values.) However, double-quotes do interpolate.
You'll need to change '/path/$name/*' to "/path/$name/*" or use string concatenation with '/path/' . $name . '/*' instead.
Also, you should be careful with accepting user-controlled input for $name as it may lead to a directory traversal attack.
I have a site where with jQuery/ajax I want to upload my image.
The problem is when I have strange filename for my image. Like with dots or other.
I have tried with this mode but doesn't work fine, it replace the dot in file extension for example if I have
image.test.png
become
imagetestpng
but I want this:
imagetest.png
This is my code:
$name = $_FILES['upload']['name'];
$size = $_FILES['upload']['size'];
$name = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $name);
echo($name);
How to solve this?
Thanks
First, you need to decompose the file name:
$info = pathinfo($name);
Then apply your filter on both parts:
$name = preg_replace("/[^\w-]+/", "", $info['filename']);
// check if we have an extension
if (isset($info['extension'])) {
$name .= '.' . preg_replace('/[^\w]/', '', $info['extension']);
}
Demo
You can use this to replace the characters in the filename while preserving the file extension.
$name = preg_replace('/[^a-zA-Z0-9_-]+/',
"",
pathinfo($name, PATHINFO_FILENAME)
) . (pathinfo($name, PATHINFO_EXTENSION)?"." . pathinfo($name, PATHINFO_EXTENSION):"");
I store the original image path(included file name) in db when upload an image.
For example:
img/uploaded/photo.jpg
Then I generate its thumbnail and store in below directory NOT in db.
/img/uploaded/thumbs/photo_thumb.jpg
And I have following function but no idea how to get the thumb which belong to the url in db.
//ie: $file is img/uploaded/photo.jpg
public function get_thumb($file)
{
//get the path without filename
$get_path = dirname($file) . "/thumbs/";
//result img/uploaded/thumbs/ (how can i get the photo_thumb.jpg) here?
return $get_path;
}
Edit
basename($file) to get filename from path but how to add _thumb.jpg?
Don't have much experience with PHP but using this as starting point, I get:
$pattern = '/^.*\/(.*)$/'; // match everything after the last slash and store it in $1
$replacement = '$1';
$filename = preg_replace($pattern, $replacement, $file);
$get_path = $file . '/thumbs/' . $filename;
As I said, not much experience with PHP, but this should do it...
A more easy way to do this, could be:
Find the last / in $file
Insert thumbs/ after it or replace it with /thumbs/
Find the last . in the edited $file
Insert _thumb after it
You can find the postitions of / and . with the strrchr function (documented here).
You can do this :
public function get_thumb($file) {
$infos = pathinfo($file);
$path = $infos['dirname'] . '/thumbs/' . $infos['filename'] . '_thumb.' . $infos['extension'];
return $path;
}
Usually, the way I do this is, is to store just the filename in the db. (assuming that they are all in the same directory).
Then query the database and get the filename, store it in:
$filename;
Then I just echo out something like
echo base_url('images/thumbs/' . $filename);
Hope this helps!
I've been trying to create a directory following a specific structure, yet nothing appears to be happening. I've approached this by defining multiple variables as follows:
$rid = '/appicons/';
$sid = '$artistid';
$ssid = '$appid';
$s = '/';
and the function I've been using runs thusly:
$directory = $appid;
if (!is_dir ($directory))
{
mkdir($directory);
}
That works. However, I want to have the following structure in created directories: /appicons/$artistid/$appid/
yet nothing really seems to work. I understand that if I were to add more variables to $directory then I'd have to use quotes around them and concatenate them (which gets confusing).
Does anyone have any solutions?
$directory = "/appicons/$artistid/$appid/";
if (!is_dir ($directory))
{
//file mode
$mode = 0777;
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
}
This should do what you want:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
$directory = $rid . $artistid . '/' . $appid . $s;
if (!is_dir ($directory)) {
mkdir($directory);
}
The reason your current code doesn't work is due to the fact you're trying to use a variable inside a string literal. A string literal in PHP is a string enclosed in single quotes ('). Every character in this string is treated as just a character, so any variables will just be parsed as text. Unquoting the variables so your declarations look like the following fixes your issue:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
This next line concatenates (joins) your variables together into a path:
$directory = $rid . $artistid . '/' . $appid . $s;
Concatenation works like this
$directory = $rid.$artistid."/".$appid."/"
When you're assigning one variable to another, you don't need the quotes around it, so the following should be what you're looking for.
$rid = 'appicons';
$sid = $artistid;
$ssid = $appid;
and then...
$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/';
if (!is_dir($dir)) {
mkdir($dir);
}
i'm trying to get google steetview tiles and save them. But file_get_contents doesn't like my url:
here's my code:
$img = "http://cbk0.google.com/cbk?output=tile&panoid="+$panorama_id+"&zoom=3&x="+$i+"&y='"+$x+"'";
$url = ""+$panorama_id+"Xval"+$i.+"Yval"+$x+".jpg";
file_put_contents($url, file_get_contents($img));
You're using + for concatenation. That's not PHP. You need the dot . operator instead:
$img = "http://cbk0.google.com/cbk?output=tile&panoid=" . $panorama_id . "&zoom=3&x=" . $i . "&y='" . $x . "'";
$url = $panorama_id . "Xval" . $i . "Yval" . $x . ".jpg";
See string operators in the PHP manual.
Alternatively, with double quoted strings, variables within the string will be parsed. See the manual page for the string type.
$img = "http://cbk0.google.com/cbk?output=tile&panoid=$panorama_id&zoom=3&x=$i&y='$x'";
$url = "${panorama_id}Xval${i}Yval${x}.jpg";