php php中move_uploaded_file的目标路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8320785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:25:50  来源:igfitidea点击:

destination path for move_uploaded_file in php

phpfile-upload

提问by luthervespers

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

现在,我有可以正常工作的代码。我的域的根目录包含以下项目(除其他外):
uploads <-这是一个文件夹
add-photo-submit.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 并添加尾部斜杠。现在在 www.mydomain.com/add-photo-submit/ 访问 PHP 文件我认为 PHP 文件将目标路径解释为“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

将来我希望我的根目录设置如下:
root
 -admin (folder)
    -add-photo-
 submit.php -uploads

What frame of reference does move_uploaded_file have?

move_uploaded_file 有什么参考系?

回答by ndlinh

You should use document_root to get absolute path like this:

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

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

回答by Marc B

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.phpinto 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/yzand 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/g/h/i/j/k/l/m/n/o/p/q/r/s/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.jpgas 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,并且您将其uploads/example.jpg用作移动路径,那么/home/sites/example.com/html/uploads/example.jpg无论请求的 URL 是什么,PHP 都会尝试将该图像放入中。

回答by Shaik Mobin

We can use dirname(__FILE__)instead of server root declare below line in any of rootfile(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"];

回答by Ar No

Or use

或使用

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

回答by Brijmohan Karadia

    $file_name ="eg-db.sql"; 
    echo "<br>";
    echo $target_file =  __DIR__.'/'.$file_name;
    echo "<br>";
    echo $target_location = __DIR__."/targetfolder/". pathinfo($target_file, PATHINFO_BASENAME); 

    echo "<br>";
    if (file_exists($target_file)){  
       echo "<br> file exist <br>";
       if (rename($target_file,$target_location)) 
       {
           echo "<br>done"; 
       }else{
           echo "<br>nothing";
       }  
    }