/    Sign up×
Community /Pin to ProfileBookmark

Path For File Uploads

Here’s a little snippet from a script that lets a user upload a file…

if($ufile) // if file exists
{
copy($ufile, $dest); //copy it to the destination
unlink ($ufile); // delete temporary file
}

“ufile” is the name of the uploaded file field as specified in the html.
The $dest variable is constructed according to the type of file uploaded.

It works a treat… but ONLY if the php script is in the root directory, even thought the target path is properly specified.

I figure it must be something to do with the relative path of the temp upload directory.

Can anyone tell me how to specify that path (or even find it) so that the script will run from any directory on my website?

Thanks
CTB

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 30.2005 — [code=php]
$tmp_path = $_SERVER['DOCUMENT_ROOT'] . '/path/to/temp/directory/from/root/';
[/code]


Edit: You can find the path to the temp directory using
[code=php]<?php
phpinfo();
?>[/code]
.

It is under environment.
Copy linkTweet thisAlerts:
@chestertbauthorMay 01.2005 — Thanks Bokeh,

I tried specifying the path to temp, /tmp, but it seems it isn't a temp directory path issue at all, as php appears to know its path to temp from where ever it is.

It seems that it's a destination directory issue.

If I specify a relative destination path, "images/pic.jpg", it works fine.

It doesn't matter where I put the upload script, as long as the destination is specified as a relative path.

If I specify path from root, in this case, "/wroof/images/pic.jpg", I get a "no such file or directory" error. The path is "correct", but clearly not for file uploads. It is tyring to draw the path from the server root, rather than the domain root?

Also... in testing this, I've noticed that uploaded files have a permission set to 644, which means they can't be overwritten. How do I

EITHER

Upload files with permission set to 777?

OR

Delete existing files before I upload the new ones?

Thanks

CTB
Copy linkTweet thisAlerts:
@bokehMay 01.2005 — [URL=http://www.php.net/chmod]www.php.net/chmod[/URL]
×

Success!

Help @chestertb 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...