/    Sign up×
Community /Pin to ProfileBookmark

change image upload path within PHP code

I have a PHP script that uploads images to a folder on my server (attachments folder). Currently the folder sits within my webroot and is publicly accessible (I have to use chmod 777 due to permissions issue). So, I created the “attachments” folder outside of my webroot (so that it is not publicly accessible), but I do not know how to set the path in the PHP code to upload it to that “attachments” folder outside of the webroot. As you see in the snippet of PHP code below, the code currently uploads the the “attachments” folder within the www (webroot) directory. How do I make it upload to the “attachments” folder OUTSIDE of the www (webroot) directory?

[code=php]foreach($files[$form] as $file){
$str = $file[1];
if (eval(“if($str){return true;}”)) {
$_values[$file[0]] = $_FILES[$file[0]][“name”];
$dirs = explode(“/”,”attachments//”);
$cur_dir =”.”;
foreach($dirs as $dir){
$cur_dir = $cur_dir.”/”.$dir;
if (!@opendir($cur_dir)) { mkdir($cur_dir, 0777);}}
$_values[$file[0].”_real-name”] = “attachments/”.date(“YmdHis”).”_”.$_FILES[$file[0]][“name”].”_secure”;
copy($_FILES[$file[0]][“tmp_name”],$_values[$file[0].”_real-name”]);
@unlink($_FILES[$file[0]][“tmp_name”]);
}else{
$flag=true;
if ($_isdisplay) {
//$ExtFltr = $file[2];
//$FileSize = $file[4];
if (!eval(“if($file[2]){return true;}”)){echo $file[3];}
if (!eval(“if($file[4]){return true;}”)){echo $file[5];}
$_ErrorList[] = $file[0];
}
}
}[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 06.2010 — You can either use a relative path, or an absolute path. The relative path might be more portable, so you could set it to something like the following, using the ".." syntax for going up the directory tree:
[code=php]
$uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/../attachments/';
[/code]
Copy linkTweet thisAlerts:
@CaptainkewlauthorOct 06.2010 — Thanks for the reply. I am a PHP novice (just learning). Do you know where I would insert that line into my PHP code that I have provided? What you said makes sense, I just don't know how to put it into my current code.

Your help is appreciated. ?
Copy linkTweet thisAlerts:
@CaptainkewlauthorOct 06.2010 — I tried this (see code) Am I even on the right track?

[code=php]foreach($files[$form] as $file){
$str = $file[1];
if (eval("if($str){return true;}")) {
$_values[$file[0]] = $_FILES[$file[0]]["name"];
$dirs = explode("['DOCUMENT_ROOT']","attachments//");
$cur_dir =".";
foreach($dirs as $dir){
$cur_dir = $_SERVER['DOCUMENT_ROOT'] . '/../attachments/';
if (!@opendir($cur_dir)) { mkdir($cur_dir, 0777);}}
$_values[$file[0]."_real-name"] = "['DOCUMENT_ROOT'] /attachments/".date("YmdHis")."_".$_FILES[$file[0]]["name"]."_secure";
copy($_FILES[$file[0]]["tmp_name"],$_values[$file[0]."_real-name"]);
@unlink($_FILES[$file[0]]["tmp_name"]);
}else{
$flag=true;
if ($_isdisplay) {
//$ExtFltr = $file[2];
//$FileSize = $file[4];
if (!eval("if($file[2]){return true;}")){echo $file[3];}
if (!eval("if($file[4]){return true;}")){echo $file[5];}
$_ErrorList[] = $file[0];
}
}
}[/code]
Copy linkTweet thisAlerts:
@NogDogOct 07.2010 — To be honest (not that I'd lie to you anyway), that whole thing looks needlessly complex to me, and I'm not entirely sure what those eval()'s are doing (and eval() can be dangerous if you aren't sure what it is evaluating). I would do something like this (but no guarantees this is correct for the rest of your code):
[code=php]
<?php
foreach($files[$form] as $file) {
if (!empty($file[1])) {
$_values[$file[0]] = $_FILES[$file[0]]["name"];
$dir = $_SERVER['DOCUMENT_ROOT'] . '/../attachments/';
if (!exists($dir) || !is_dir($dir) || !is_writable($dir)) {
user_error("Attachments directory does not exist or is not writable");
} else {
$_values[$file[0] . "_real-name"] = $dir . date("YmdHis") . "_" . $_FILES[$file[0]]["name"] . "_secure";
if (move_uploaded_file($_FILES[$file[0]]["tmp_name"], $_values[$file[0] . "_real-name"]) == false) {
user_error("Unable to save uploaded file");
}
}
} else {
$flag = true;
if ($_isdisplay) {
//$ExtFltr = $file[2];
//$FileSize = $file[4];
if (empty($file[2])) {
echo $file[3];
}
if (empty($file[4])) {
echo $file[5];
}
$_ErrorList[] = $file[0];
}
}
}
[/code]
×

Success!

Help @Captainkewl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...