php - how to get current dir of server but not current filename? - php

SOLUTION
$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$feedtitle = str_replace(" ", "", $feedtitle);
$feedtitle = strtolower($feedtitle);
$path = substr($path, 0, strrpos($path, "/"));
$feedlink = "http://" . "$path" . "/" . "$feedtitle" . '.xml';
PROBLEM
I am trying to generate a name for an rss feed from another field (feed title). In my query I have :
$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$feedtitle = str_replace(" ", "", $feedtitle);
$feedtitle = strtolower($feedtitle);
$feedlink = "$path" . "$feedtitle" . '.xml';
If feedtitle is my rss feed, $feedlink will have the value myrssfeed.xml so that part of the code works but I'm having problems with the path. Instead of writing the current path the above writes the current path & the name of the current file! e.g. if the script I'm using for this is named feedlink.php the path that is stored in the $feedlink variable is :
mywebsite.com/mydir/feedlink.phpmyrssfeed.xml
How do I remove the name of the script?

With the str_replace() function
$feedlink = str_replace("feedlink.php", "", $feedlink);

Assuming that you requested the site as mywebsite.com/mydir/feedlink.php you should consider removing everything past the last / and then add your filename to that:
$path = substr($path, 0, strrpos($path, "/") + 1);

Related

How to create dynamic path using php variable?

I want to create a path with dynamic variable.
echo $path='App\Widgets\mywidgetname';
I want to replace mywidgetname then I have to put a variable like this
$path='App\Widgets\'.$widgetname; // (but this not working)
Try this,
$path = 'App\Widgets\mywidgetname';
$path = str_replace('mywidgetname', $widgetname, $path);
OR
$path = 'App\Widgets\mywidgetname';
$path = substr($path, 0, strrpos($path, "\\") + 1) . $widgetname;
You can use:
$path = sprintf("App\Widgets\%s", $widgetname);
echo $path;

Extract specific part from URL

I have to extract a specific part of an URL.
Example
original URLs
http://www.example.com/PARTiNEED/some/other/stuff
http://www.example.com/PARTiNEED
in case 1 I need to extract
/PARTiNEED/
and in case 2 I need to extract the same part but add an additional "/" at the end
/PARTiNEED/
What I've got right now is this
$tempURL = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$tempURL = explode('/', $tempURL);
$tempURL = "/" . $tempURL[3] . "/";
is there a more convenient way to do this or is this solution fine?
It's normally a good idea to use PHP's built in functions for things like this where possible. In this case, the parse_url method is designed for parsing URLs.
In your case:
// Extract the path from the URL
$path = parse_url($url, PHP_URL_PATH);
// Separate by forward slashes
$parts = explode('/', $path);
// The part you want is index 1 - the first is an empty string
$result = "/{$parts[1]}/";
You don't need this part:
'http://'. $_SERVER['SERVER_NAME']
you can just do:
$tempURL = explode('/', $_SERVER['REQUEST_URI']);
$tempURL = "/" . $tempURL[1] . "/";
Edited index from 0 to 1 as commented.
Maybe regex suits your needs better?
$tempURL = "http://www.example.com/PARTiNEED/some/other/stuff"; // or $tempURL = "http://www.example.com/PARTiNEED
$pattern = '#(?<=\.com)(.+?)(?=/|$)#';
preg_match($pattern, $tempURL, $match);
$result = $match[0] . "/";
Here this should solve your problem
// check if the var $_SERVER['REQUEST_URI'] is set
if(isset($_SERVER['REQUEST_URI'])) {
// explode by /
$tempURL = explode('/', $_SERVER['REQUEST_URI']);
// what you need in in the array $tempURL;
$WhatUNeed = $tempURL[1];
} else {
$WhatUNeed = '/';
}
Dont worry about the trailing slash, that can be added anytime in your code.
$WhatUNeed = $tempURL[1].'/';
This will give you proper idea about your requirment.
<?php
$url_array = parse_url("http://www.example.com/PARTiNEED/some/other/stuff");
$path = $url_array['path'];
var_dump($path);
?>
now you can use string explode function to get your job done.

Php url parsing and editing

I searched "the whole" stackoverflow but didn't find a decent answer that works for me. I need to change the host of a url in php.
This url: http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126
To This: http://example456.com/test?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126
I only need to change the domain and the path or file, so far I've got this:
$originalurl = http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400....
$parts = parse_url($originalurl);
$parts['host'] = $_SERVER['HTTP_HOST'];
$parts['path'] = '/test';
$modifiedurl = http_build_query($parts);
print_r(urldecode($modifiedurl));
but it echos
scheme=http&host=localhost&path=/test&query=t=de&p=9372&pl=bb02799a&cat=&sz=400...
Please I don't want to use some strpos or something like that as I need it to be variable.
Thanks ;)
$url = 'http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126';
$query = parse_url($url)['query'];
$newUrl = 'http://www.younewdomain.com/path?' . $query;
You'll have to do some concatenating manually. This works:
$originalurl = "http://example123.com/query?t=de&p=9372";
$parts = parse_url($originalurl);
$new_path = '/test';
$modifiedurl = $parts['scheme'] . "://" . $_SERVER['HTTP_HOST'] . $new_path . (isset($parts['query']) ? "?".$parts['query']:"");
print_r($modifiedurl);
Came up with a different approach:
$url = "http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126";
$new_host = "http://newhost.com/blab";
//explode at ? so you get the query
$split = explode("?",$url,2);
//build new url
$new_url = $new_host."?".$split[1];
//finish
echo $new_url;
The reverse function of parse_url() should be http_build_url(), have you tried with it?

Regular expression filename

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):"");

Creating a directory in PHP based on a bunch of variables

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);
}

Categories