简体   繁体   English

如何在PHP中设置相对路径?

[英]How do I set relative paths in PHP?

I have an absolute path of (verified working) 我有一个绝对的路径(经过验证的工作)

  $target_path = "F:/xampplite/htdocs/host_name/p/$email.$ext";

for use in 用于

move_uploaded_file($_FILES['ufile']['tmp_name'], $target_path

However when I move to a production server I need a relative path: 但是,当我移动到生产服务器时,我需要一个相对路径:

If /archemarks is at the root directory of your server, then this is the correct path. 如果/archemarks位于服务器的根目录中,那么这是正确的路径。 However, it is often better to do something like this: 但是,做这样的事情往往更好:

$new_path = dirname(__FILE__) . "/../images/" . $new_image_name;

This takes the directory in which the current file is running, and saves the image into a directory called images that is at the same level as it. 这将获取当前文件运行的目录,并将图像保存到与其处于同一级别的名为images的目录中。

In the above case, the currently running file might be: 在上面的例子中,当前运行的文件可能是:

/var/www/archemarks/include/upload.php

The image directory is: 图像目录是:

/var/www/archemarks/images

For example, if /images was two directory levels higher than the current file is running in, use 例如,如果/images是两个目录级别高于当前运行的文件,请使用

$new_path = dirname(__FILE__) . "/../../images/" . $new_image_name;
$target_path = __DIR__ . "/archemarks/p/$email.$ext";
$target_path = "archemarks/p/$email.$ext";

notice the first "/" 注意第一个“/”

/ => absolute, like /home / =>绝对,像/ home

no "/" => relative to current folder 没有“/”=>相对于当前文件夹

That is an absolute path. 这是一条绝对的道路。 Relative paths do not begin with a / . 相对路径不以/开头。

If this is the correct path for you on the production server, then PHP may be running in a chroot . 如果这是生产服务器上的正确路径,那么PHP可能正在chroot运行。 This is a server configuration issue. 这是服务器配置问题。

Below is code for a php file uploader I wrote with a relative path. 下面是我用相对路径编写的php文件上传器的代码。 Hope it helps. 希望能帮助到你。 In this case, the upload folder is in the same dir as my php file. 在这种情况下,上传文件夹与我的php文件在同一个目录中。 you can go up a few levels and into a different dir using ../ 你可以使用../上升几个级别并进入另一个目录

<?php
if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get"))
@date_default_timezone_set('America/Anchorage');
ob_start();
session_start();

// Where the file is going to be placed 
$target_path = "uploads/" . date("Y/m/d") . "/" . session_id() . "/";
if(!file_exists( $target_path )){
    if (!mkdir($target_path, 0755, true))
        die("FAIL: Failed to create folders for upload.");
}

$maxFileSize = 1048576*3.5; /* in bytes */ 

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$index = 0;
$successFiles = array();
$failFiles = null;
$forceSend = false;
if($_SESSION["security_code"]!==$_POST["captcha"]){
    echo "captcha check failed, go back and try again";
    return;
}

foreach($_FILES['attached']['name'] as $k => $name) {
    if($name != null && !empty($name)){
        if($_FILES['attached']['size'][$index] < $maxFileSize ) {
            $tmp_target_path = $target_path . basename( $name );
            if(move_uploaded_file($_FILES['attached']['tmp_name'][$index], $tmp_target_path)) {
                $successFiles[] = array("file" => $tmp_target_path);
            } else{
                if($failFiles == null){
                    $failFiles = array();
                }
                $failFiles[] = array ("file" => basename( $name ), "reason" => "unable to copy the file on the server");
            }
        } else {
            if($failFiles == null){
                    $failFiles = array();
                }
                $failFiles[] = array ("file" => basename( $name ), "reason" => "file size was greater than 3.5 MB");
        }
        $index++;
    }   
}

?>

<?php 
    $response = "OK";
    if($failFiles != null){ 
        $response = "FAIL:" . "File upload failed for <br/>";
        foreach($failFiles as $k => $val) {
            $response .= "<b>" . $val['file'] . "</b> because " . $val['reason'] . "<br/>";
        }
    }

?>
<script language="javascript" type="text/javascript">
   window.top.window.uploadComplete("<?php echo $response; ?>");
</script>  

Assuming the /archemarks directory is directly below document root - and your example suggests that it is -, you could make the code independent of a specific OS or environment. 假设/ archemarks目录直接位于文档根目录下 - 并且您的示例表明它是 - ,您可以使代码独立于特定的操作系统或环境。 Try using 尝试使用

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/archemarks/p/$email.$ext";

as a generic path to your target location. 作为目标位置的通用路径。 Should work fine. 应该工作正常。 This notation is also independent of the location of your script, or the current working directory. 此表示法也与脚本的位置或当前工作目录无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM