6

我正在使用 php 上传文件并将其移动到所需位置...在使用 move_uploaded_file 时,它​​表示文件已成功移动,但文件未显示在目录中。HTML 和 PHP 代码如下...

 <form action="test_upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<label for="test_pic">Testing Picture</label>
<input type="file" name="test_pic" size="30" /><br />
</fieldset>

<fieldset>

<input type="submit" value="submit" />

</fieldset>
</form>

php 是这样的:

    <?php


$image_fieldname = "test_pic";
$upload_dir = "/vidit";
$display_message ='none';

if(move_uploaded_file($_FILES[$image_fieldname]['tmp_name'],$upload_dir) && is_writable($upload_dir)){
    $display_message = "file moved successfully";
    }
    else{
        $display_message = " STILL DID NOT MOVE";
            }



?>

当我运行此页面并上传合法文件时 - test_upload.php 回显文件已成功上传。但是当我前往网页根目录中的文件夹“vidit”时。文件夹是空的...

我正在使用 wamp 服务器。

4

4 回答 4

4

您需要将文件名附加到目标路径中。尝试如下

$doc_path = realpath(dirname(__FILE__));

if(move_uploaded_file($_FILES[$image_fieldname]['tmp_name'],$doc_path.$upload_dir.'/'.$_FILES[$image_fieldname]['name']) && is_writable($upload_dir)){
    $display_message = "file moved successfully";
}
else{
    $display_message = " STILL DID NOT MOVE";
}

请参阅 PHP 手册以供参考。http://php.net/manual/en/function.move-uploaded-file.php

于 2012-09-30T06:53:45.370 回答
0

使用它可能会起作用

$upload_dir = "vidit/";



      $uploadfile=($upload_dir.basename($_FILES['test_pic']['name']));

if(move_uploaded_file($_FILES['test_pic']['tmp_name'], $uploadfile) ) {


}
于 2012-09-30T06:52:19.517 回答
0
<?php
  $image_fieldname = "test_pic";
  $upload_dir = "/vidit";
  $display_message ='none';
  if (move_uploaded_file($_FILES[$image_fieldname]['tmp_name'],$upload_dir . '/' . $_FILES[$image_fieldname]['name'] && is_writable($upload_dir)) {
    $display_message = "file moved successfully";
  } else {
    $display_message = " STILL DID NOT MOVE";
  }
?>

还添加了缩进。需要应用文件名,或者就像将文件作为无扩展名文件上传一样。

于 2012-09-30T07:14:42.380 回答
0

在 root(www) 中创建 tmp 目录并更改目录路径绝对有效/在您的目录名称是强制性的之后

$upload_dir = $_SERVER['DOCUMENT_ROOT'].'/tmp/';

于 2016-10-09T08:57:02.657 回答