1

我想用 PHP 上传文件。我写

<form target="_self" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input name="submit" type="submit" value="submit" />
</form>
<?php

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
  }

?>

但它不起作用。我看不到文件,它们要去哪里?

4

3 回答 3

4

你离工作脚本还有很长的路要走

你可以试试

<?php
$output = array ();
$errors = array ();
$savePath = "uploaded"; // PATH to upload images
$allowedExt = array (
        "png",
        "jpg" 
);
if (isset ( $_FILES ['file'] ) && $_FILES ["file"] ["error"] == UPLOAD_ERR_OK) {

    $fileName = $_FILES ['file'] ['name'];
    $fileSize = $_FILES ['file'] ['size'];
    $fileTemp = $_FILES ['file'] ['tmp_name'];
    $fileType = $_FILES["file"]["type"] ;
    $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
    $fileExt = strtolower ( $fileExt );

    //var_dump($fileExt);

    if (!in_array ( $fileExt, $allowedExt )) {
        $errors [] = "Invalid File Extention";
    }

    if ($fileSize > 800*1024) {
        $errors [] = "File Too large";
    }

    if (! is_writable ( $savePath )) {
        $errors [] = "File Destination not writeable";
    }

    $fileDst = $savePath . DIRECTORY_SEPARATOR . $fileName;
    $filePrifix = basename ( $fileName, "." . $fileExt );
    $i = 0;
    while ( file_exists ( $fileDst ) ) {
        $i ++;
        $fileDst = $savePath . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;

    }
    if (count ( $errors ) == 0) {
        if (@move_uploaded_file ( $fileTemp, $fileDst )) {
            $output['STATUS'] = "OK";
            $output['TYPE'] = $fileType;
            $output['Size'] = $fileSize;
            $output['Destination'] = $fileDst;
        } else {
            $errors [] = "Error Saving File";
        }

    }


    if(count($errors) > 0)
    {
        echo "<h2>Upload Error</h2>" ;

        foreach ($errors as $error)
        {
            echo $error , "<br/>" ;

        }
    }
    else
    {
        echo "<h2>File  Uploaded</h2>" ;
        foreach ($output as $key => $value)
        {
            echo $key , ":" , $value , "<br/>" ;
        }

    }
}

?>
<br/>
<br />
<form method="post" target="_self" enctype="multipart/form-data">
    <input type="file" name="file" id="file" /> <input name="submit"
        type="submit" value="submit" />
</form>
于 2012-04-22T19:24:26.140 回答
3

您的代码甚至还没有接近工作,因为您实际上并没有通过 PHP 上传任何文件。我建议从PHP 文件上传教程更多)开始,然后再试一次。如果您对新代码有任何问题,请寻求帮助并向我们展示您遇到问题的代码。

于 2012-04-22T17:29:32.147 回答
0

您需要使用 POST 方法才能使文件上传正常工作。
因此,只需将method="post"属性添加到form标签即可。

于 2012-04-22T18:24:19.393 回答