Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

file upload - destination path for move_uploaded_file in php

I need to know what to use for a destination path for PHP's move_uploaded_file function. (see http://php.net/manual/en/function.move-uploaded-file.php)

Right now, I have code that works fine. My domain's root directory contains the following items (among others):
uploads     <-this is a folder
add-photo-submit.php    <-this is a PHP file that uses move_uploaded_file

In add-photo-submit.php, I have the following line of code:

$target_path = "uploads/" . basename($_FILES['uploadedFile']['name']);

$target_path is used as the destination parameter for the function. This works just fine when I access the file through www.mydomain.com/add-photo-submit.php

However, I recently changed the .htaccess file to remove the .php and add a trailing slash. Now the PHP file is accessed at www.mydomain.com/add-photo-submit/ I think the PHP file is interpreting the target path as "www.mydomain.com/add-photo-submit/uploads/filename.jpg"

I tried using an absolute path, but it said I didn't have permission...

In the future I would like my root directory to be setup like this:
root
 -admin (folder)
    -add-photo-submit.php
 -uploads

What frame of reference does move_uploaded_file have?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use document_root to get absolute path like this:

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . basename($_FILES['uploadedFile']['name']);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...