这令我感到困惑。出于某些奇怪的原因,move_uploaded_file()不会将文件放入目录中,但它会返回有效的tmp_name和name。所以在这种情况下,$ file always = to false。
无论如何,有人能指出这段代码的一些问题吗?非常感谢。
<?php
include 'realtydevkit.php';
session_start();
$name = $_FILES['yourlogo']['name'];
$tmpname = $_FILES['yourlogo']['tmp_name'];
if ($name) {
$directory = $name;
$userid = $_SESSION['userid'];
$type = "logo";
$file = move_uploaded_file($tmpname, $directory);
if ($file == true) {
mysql_query("INSERT INTO usercontent
(`userid`, `type`, `url`) VALUES
('$userid', '$type', '$directory')");
echo 'Uploaded';
echo "<img src='".$directory."'/>";
} else {
echo 'There was an error moving the file.';
}
}
?>
答案 0 :(得分:1)
您也没有检查$_FILES['yourlogo']['error']
。
以下是您使用上传文件检查的内容:
$_FILES['yourlogo']['error']
。如果是UPLOAD_ERR_OK
(0),则继续。is_uploaded_file($_FILES['yourlogo']['tmp_name'])
。如果失败,PHP不知道出了什么问题,但它不信任该文件。move_uploaded_file()
。如果move_uploaded_file()
错误,那么PHP无法在目的地写入文件。这通常是无效路径,或者没有权限。
请注意,$_FILES['yourlogo']['name']
是文件在上传者PC上的名称。它不应该有任何路径信息。
答案 1 :(得分:0)
<?php
include 'realtydevkit.php';
session_start();
$name = $_FILES['yourlogo']['name'];
$tmpname = $_FILES['yourlogo']['tmp_name'];
if ($name) {
$directory = $name;
$userid = $_SESSION['userid'];
$type = "logo";
$file = move_uploaded_file($tmpname, "path of ur directore/".$directory);
if ($file == true) {
mysql_query("INSERT INTO usercontent
(`userid`, `type`, `url`) VALUES
('$userid', '$type', '$directory')");
echo 'Uploaded';
echo "<img src='".$directory."'/>";
}
else
{
echo 'There was an error moving the file.';
}
}
?>