【发布时间】:2012-10-02 23:55:33
【问题描述】:
表单的php文件是:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 60000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("http://example.me/imgs/examples" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"http://example.me/imgs/examples" . $_FILES["file"]["name"]);
echo "Stored in: " . "http://example.me/imgs/examples" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
这很简单,对吧?表单的html是:
<form action="img_upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit"/>
</form>
也足够简单;正确的?提交图像后,我被重定向到 img_upload.php 文件,并显示以下文本:
Upload: example.jpg
Type: image/jpeg
Size: 39.1943359375 Kb
Temp file: C:\WINDOWS\Temp\php66.tmp
Stored in: http://example.me/imgs/examplesexample.jpg
当我尝试执行这样的简单任务并且没有错误但也没有结果时,我感到非常沮丧。我已经尝试在 php 脚本中添加像 imgs/examples/ 这样的“/”,唯一的区别是提交后的以下行是:
Stored in: http://example.me/imgs/examples/example.jpg
关于我在这里缺少什么的任何想法。我有许多其他 php 脚本/mysql 表正在使用和上传图像,但对于这个简单的任务什么都没有。我在 localhost 上的 apache 服务器上使用 php 5 进行测试。提前感谢大家的任何帮助。
【问题讨论】:
-
我认为您的服务器中没有上传权限。使用网站管理面板进行检查。
-
您是否拥有上传图片所需的文件夹,您是否有权写入该目录?
-
我没有管理面板。我直接在 apache 的配置文件、虚拟主机配置文件或 php 的 ini 文件中进行所有更改。
-
@Alex 是的,我有权限,正如我所说,我在其他脚本中拥有通过 php/mysql 创建的小型 cms,其中图像存储在目录中并且可以正常工作。我想要上传图像的文件夹也确实存在。这就是我感到沮丧的原因。