Creating new variable from another via string manipulation
Suppose, I have a variable $var1 which contains the followings:
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
Now, I want to create another variable $var2 which will hold the following value:
$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";
I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?
php string
add a comment |
Suppose, I have a variable $var1 which contains the followings:
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
Now, I want to create another variable $var2 which will hold the following value:
$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";
I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?
php string
I'd refactor the whole system so you'd have just:$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.
– Ultimater
Nov 22 '18 at 6:54
add a comment |
Suppose, I have a variable $var1 which contains the followings:
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
Now, I want to create another variable $var2 which will hold the following value:
$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";
I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?
php string
Suppose, I have a variable $var1 which contains the followings:
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
Now, I want to create another variable $var2 which will hold the following value:
$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";
I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?
php string
php string
edited Nov 22 '18 at 6:37
Magnus Eriksson
7,42641328
7,42641328
asked Nov 22 '18 at 6:26
user10689421user10689421
1
1
I'd refactor the whole system so you'd have just:$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.
– Ultimater
Nov 22 '18 at 6:54
add a comment |
I'd refactor the whole system so you'd have just:$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.
– Ultimater
Nov 22 '18 at 6:54
I'd refactor the whole system so you'd have just:
$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or ../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.– Ultimater
Nov 22 '18 at 6:54
I'd refactor the whole system so you'd have just:
$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or ../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.– Ultimater
Nov 22 '18 at 6:54
add a comment |
4 Answers
4
active
oldest
votes
Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:
$var2 = str_replace('../', '../dev/', $var1);
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of../
throughout the string, since we don't know how user-uploaded filenames are being handled.
– kungphu
Nov 22 '18 at 6:55
add a comment |
It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php
and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php
would be an obvious target.
You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.
add a comment |
you could also use strtr() function of PHP
$var2 = strtr($var1, '../', '../dev/');
add a comment |
I'd approach it by separating out the file name using basename()
and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
$devpath = "../dev/sidebar_items/uploaded_files/notices/";
echo $devpath.basename($var1);
gives...
../dev/sidebar_items/uploaded_files/notices/circular.pdf
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425016%2fcreating-new-variable-from-another-via-string-manipulation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:
$var2 = str_replace('../', '../dev/', $var1);
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of../
throughout the string, since we don't know how user-uploaded filenames are being handled.
– kungphu
Nov 22 '18 at 6:55
add a comment |
Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:
$var2 = str_replace('../', '../dev/', $var1);
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of../
throughout the string, since we don't know how user-uploaded filenames are being handled.
– kungphu
Nov 22 '18 at 6:55
add a comment |
Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:
$var2 = str_replace('../', '../dev/', $var1);
Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:
$var2 = str_replace('../', '../dev/', $var1);
answered Nov 22 '18 at 6:32
Magnus ErikssonMagnus Eriksson
7,42641328
7,42641328
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of../
throughout the string, since we don't know how user-uploaded filenames are being handled.
– kungphu
Nov 22 '18 at 6:55
add a comment |
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of../
throughout the string, since we don't know how user-uploaded filenames are being handled.
– kungphu
Nov 22 '18 at 6:55
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of
../
throughout the string, since we don't know how user-uploaded filenames are being handled.– kungphu
Nov 22 '18 at 6:55
I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of
../
throughout the string, since we don't know how user-uploaded filenames are being handled.– kungphu
Nov 22 '18 at 6:55
add a comment |
It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php
and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php
would be an obvious target.
You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.
add a comment |
It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php
and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php
would be an obvious target.
You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.
add a comment |
It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php
and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php
would be an obvious target.
You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.
It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php
and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php
would be an obvious target.
You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.
answered Nov 22 '18 at 6:52
kungphukungphu
2,81911425
2,81911425
add a comment |
add a comment |
you could also use strtr() function of PHP
$var2 = strtr($var1, '../', '../dev/');
add a comment |
you could also use strtr() function of PHP
$var2 = strtr($var1, '../', '../dev/');
add a comment |
you could also use strtr() function of PHP
$var2 = strtr($var1, '../', '../dev/');
you could also use strtr() function of PHP
$var2 = strtr($var1, '../', '../dev/');
answered Nov 22 '18 at 7:14
Parvej AlamParvej Alam
2168
2168
add a comment |
add a comment |
I'd approach it by separating out the file name using basename()
and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
$devpath = "../dev/sidebar_items/uploaded_files/notices/";
echo $devpath.basename($var1);
gives...
../dev/sidebar_items/uploaded_files/notices/circular.pdf
add a comment |
I'd approach it by separating out the file name using basename()
and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
$devpath = "../dev/sidebar_items/uploaded_files/notices/";
echo $devpath.basename($var1);
gives...
../dev/sidebar_items/uploaded_files/notices/circular.pdf
add a comment |
I'd approach it by separating out the file name using basename()
and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
$devpath = "../dev/sidebar_items/uploaded_files/notices/";
echo $devpath.basename($var1);
gives...
../dev/sidebar_items/uploaded_files/notices/circular.pdf
I'd approach it by separating out the file name using basename()
and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...
$var1="../sidebar_items/uploaded_files/notices/circular.pdf";
$devpath = "../dev/sidebar_items/uploaded_files/notices/";
echo $devpath.basename($var1);
gives...
../dev/sidebar_items/uploaded_files/notices/circular.pdf
answered Nov 22 '18 at 7:21
Nigel RenNigel Ren
28.2k62034
28.2k62034
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425016%2fcreating-new-variable-from-another-via-string-manipulation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd refactor the whole system so you'd have just:
$filename="/sidebar_items/uploaded_files/notices/circular.pdf";
The responsibility of deciding what directory to look in (..
or../dev
) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.– Ultimater
Nov 22 '18 at 6:54