PHP文件上传

时间:2011-12-31 06:32:59

标签: php

嘿伙计们,我正在尝试在我的网站上创建一个文件上传器,我已经按照教程进行了操作,但似乎没有用。我目前正在使用以下代码:

    <?php
session_start();
include('../../config/config.php');

if(isset($_SESSION['id'])) {

$filename = $_FILES["file"]["name"];
$filesize = $_FILES["file"]["size"];

$name = $_POST['name'];
$price = "$" . $_POST['price'];
$photo = $_POST['file'];
$description = nl2br($_POST['description']);

$file_ext = getExt($photo);

  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("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }

} else {

header("Location: ../../index.php");

}


?>

我似乎创建了临时文件,但我不确定为什么我不会移动它。现在我正在用图像文件测试代码。

此处还有我的表格代码:

                <form method="post" action="actions/add.php" enctype="multipart/form-data">
                    </br>
                    Listing name: <input type="text" name="name" /></br></br>
                    Listing Photo: <input type="file" name="file"/></br></br>
                    Price: $<input type="text" name="price"/></br></br>
                    Information:</br>
                    <textarea class="add" name="description"></textarea>

                    </br></br>

                    <input class="add" type="submit" name="submit"/>
                </form>

编辑:

一些错误:

    Warning: move_uploaded_file(upload/Corgi 004.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/stationr/public_html/admin/actions/add.php on line 42

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/chroot/tmp/phpLXObbt' to 'upload/Corgi 004.jpg' in /home/stationr/public_html/admin/actions/add.php on line 42

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

$filename = $_FILES["file"]["name"];
$filesize = $_FILES["file"]["size"];

$name = $_POST['name'];
$price = "$" . $_POST['price'];
//$photo = $_POST['file'];
$description = nl2br($_POST['description']);

//$file_ext = getExt($photo);

  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 />";

$newname = dirname(__FILE__).'/upload/'.$filename;

if (file_exists($newname))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],$newname);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}