php中move_uploaded_file的目标路径

时间:2022-04-12 16:04:24

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)

我需要知道PHP的move_uploaded_file函数的目标路径是什么。(参见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

现在,我有可以正常工作的代码。我的域的根目录包含以下条目(包括其他):上传<-这是一个文件夹add- photosubmit。php <-这是一个使用move_uploaded_file的php文件

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

在add-photo-submit。php,我有以下一行代码:

$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

$target_path用作函数的目标参数。当我通过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"

但是,我最近修改了.htaccess文件,删除了.php并添加了一个斜杠。现在PHP文件在www.mydomain.com/add-photo-submit/我认为PHP文件将目标路径解释为www.mydomain.com/add- photomit/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

在将来,我希望我的根目录像这样设置:root -admin(文件夹)-添加照片提交。php上传

What frame of reference does move_uploaded_file have?

move_uploaded_file有什么引用框架?

4 个解决方案

#1


24  

You should use document_root to get absolute path like this:

您应该使用document_root来获得这样的绝对路径:

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

#2


1  

PHP's local filename operations have no idea what mod_rewrite did to your urls, and deal only with actual raw file system paths. You could have mod_rewrite turn your nice simple example.com/index.php into a hideous example.com/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/yz and PHP would still be running in the same physical directory on the server.

PHP的本地文件名操作不知道mod_rewrite对url做了什么,只处理原始文件系统路径。你可以让mod_rewrite你那漂亮的example.com/index.php变成一个丑陋的example.com/a/b/c/d/e/f/f/g/h/h/i /k/ l/l/ml /o/ q/ q/r/ t/u/v/w/x/yz和PHP仍然在服务器上的同一个物理目录下运行。

If the PHP script lives at /home/sites/example.com/html/index.php, and you use uploads/example.jpg as your move path, then PHP will attempt to put that image into /home/sites/example.com/html/uploads/example.jpg, regardless of what the requested URL was.

如果PHP脚本存在于/home/sites/example.com/html/index.php,并且您使用upload /example.jpg作为您的移动路径,那么PHP将尝试将该图像放入/home/sites/example.com/html/uploads/example.jpg,而不管请求的URL是什么。

#3


0  

Or use

或使用

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

#4


0  

We can use dirname(__FILE__) instead of server root declare below line in any of root file(index.php)

我们可以在任何根文件(index.php)中使用dirname(__FILE__)而不是在根文件(index.php)行下面声明服务器根

$_SESSION["uploads_base_url"]=dirname(__FILE__); 

and you can now use this in any files where upload is needed.

你现在可以在任何需要上传的文件中使用它。

echo $uploads_base_url=$_SESSION["uploads_base_url"];

#1


24  

You should use document_root to get absolute path like this:

您应该使用document_root来获得这样的绝对路径:

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

#2


1  

PHP's local filename operations have no idea what mod_rewrite did to your urls, and deal only with actual raw file system paths. You could have mod_rewrite turn your nice simple example.com/index.php into a hideous example.com/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/yz and PHP would still be running in the same physical directory on the server.

PHP的本地文件名操作不知道mod_rewrite对url做了什么,只处理原始文件系统路径。你可以让mod_rewrite你那漂亮的example.com/index.php变成一个丑陋的example.com/a/b/c/d/e/f/f/g/h/h/i /k/ l/l/ml /o/ q/ q/r/ t/u/v/w/x/yz和PHP仍然在服务器上的同一个物理目录下运行。

If the PHP script lives at /home/sites/example.com/html/index.php, and you use uploads/example.jpg as your move path, then PHP will attempt to put that image into /home/sites/example.com/html/uploads/example.jpg, regardless of what the requested URL was.

如果PHP脚本存在于/home/sites/example.com/html/index.php,并且您使用upload /example.jpg作为您的移动路径,那么PHP将尝试将该图像放入/home/sites/example.com/html/uploads/example.jpg,而不管请求的URL是什么。

#3


0  

Or use

或使用

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

#4


0  

We can use dirname(__FILE__) instead of server root declare below line in any of root file(index.php)

我们可以在任何根文件(index.php)中使用dirname(__FILE__)而不是在根文件(index.php)行下面声明服务器根

$_SESSION["uploads_base_url"]=dirname(__FILE__); 

and you can now use this in any files where upload is needed.

你现在可以在任何需要上传的文件中使用它。

echo $uploads_base_url=$_SESSION["uploads_base_url"];