我有一个绝对的路径(经过验证的工作)
$target_path = "F:/xampplite/htdocs/host_name/p/$email.$ext";
用于
move_uploaded_file($_FILES['ufile']['tmp_name'], $target_path
然而,当我搬到生产服务器时,我需要一个相对路径:
答案 0 :(得分:3)
如果/archemarks
位于服务器的根目录中,那么这是正确的路径。但是,做这样的事情往往更好:
$new_path = dirname(__FILE__) . "/../images/" . $new_image_name;
这将获取当前文件运行的目录,并将图像保存到名为images
的目录中,该目录与它处于同一级别。
在上述情况下,当前运行的文件可能是:
/var/www/archemarks/include/upload.php
图像目录是:
/var/www/archemarks/images
例如,如果/images
两个目录级别高于当前运行的文件,请使用
$new_path = dirname(__FILE__) . "/../../images/" . $new_image_name;
答案 1 :(得分:2)
$target_path = __DIR__ . "/archemarks/p/$email.$ext";
答案 2 :(得分:1)
$target_path = "archemarks/p/$email.$ext";
注意第一个" /"
/ =>绝对的,比如/ home
不" /" =>相对于当前文件夹
答案 3 :(得分:1)
这是绝对路径。相对路径不以/
开头。
如果这是生产服务器上的正确路径,那么PHP可能正在chroot
中运行。这是服务器配置问题。
答案 4 :(得分:1)
下面是我用相对路径编写的php文件上传器的代码。 希望能帮助到你。在这种情况下,上传文件夹与我的php文件在同一个目录中。您可以使用../
上升几个级别并进入不同的目录<?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>
答案 5 :(得分:1)
假设/ archemarks目录直接位于文档根目录下 - 并且您的示例表明它是 - ,您可以使代码独立于特定的操作系统或环境。尝试使用
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/archemarks/p/$email.$ext";
作为目标位置的通用路径。应该工作正常。此表示法也与脚本的位置或当前工作目录无关。