PHP文件上传无法使用动态路径

时间:2010-04-04 14:19:22

标签: php file-upload

我正在尝试编写一个php上传脚本来将文件上传到不同的文件夹。我的代码适用于直接路径(类似'path / to / directory'),但不适用于从运行时获取的动态路径。

$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder;
$uploadsDirectory = str_replace (" ", "", $toupload);

当我回显$ uploadsDirectory时,它会显示确切的路径。任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

尝试使用dirname(__FILE__);

<?php
$directory_self = dirname(__FILE__);
$folder = "faruk"; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files/'. $folder;
$uploadsDirectory = trim($toupload);
echo $uploadsDirectory."\n";

?>

我的笔记本电脑输出;

/home/test/Desktop/files/test

答案 1 :(得分:0)

您应该检查并查看是否已创建文件夹,以及脚本是否有权在其中写入文件。

上传脚本的确切输出是什么? (即它会抛出什么错误?)

答案 2 :(得分:0)

尝试添加一些调试内容,以查看您生成的路径是否实际存在且可写:

$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '/files';

$uploadsDirectory = str_replace (" ", "", $uploaddir);

if (!is_dir($uploadDirectory)) {
    die("$uploadDirectory is not a directory");
}

if (!is_writeable($uploadDirectory)) {
    die("$uploaddir is not writeable");
}

$toupload = $uploadDirectory . $folder;

if (!is_writeable($toupload)) {
    die("$toupload is not writeable");
}