使用php在文件上传期间显示警告消息

时间:2015-02-18 10:12:08

标签: php file-upload

我在XAMPP上使用ubuntu os。在这里,我创建了两个文件uploader.php和phpEx6.php 当我尝试上传文件时,它显示警告信息。我是php.pls的新手帮助解决问题..

phpEx6.php

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploader.php" method="post" enctype="multipart/form-   data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

uploader.php

<?php
if( $_FILES['file']['name'] != "" )
{
   error_reporting(E_ALL);
move_uploaded_file($_FILES['file']['tmp_name'],     $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/' $_FILES['file']['name'])
or die("Couldn't copy the file.");

}
else
{
    die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>enter code here
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
</ul>
</body>
</html>

输出

警告:move_uploaded_file(/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt):无法打开流:/ opt / lampp / htdocs / xampp中没有此类文件或目录第5行的/nf/uploader.php

警告: move_uploaded_file():无法移动&#39; / opt / lampp / temp / php1BAGC1&#39; to&#39; /opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt'在第5行的/opt/lampp/htdocs/xampp/nf/uploader.php

4 个答案:

答案 0 :(得分:1)

尝试改变这个

move_uploaded_file($_FILES['file']['tmp_name'],     $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])

用这个

move_uploaded_file($_FILES['file']['tmp_name'],     $_SERVER['DOCUMENT_ROOT'] . '/xampp/nf/uploads/'. $_FILES['file']['name'])

甚至是

move_uploaded_file($_FILES['file']['tmp_name'],   '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])

答案 1 :(得分:0)

错误是,没有创建文件夹,你试图移动文件..创建文件夹,然后尝试它或检查文件夹是否具有上传文件所需的所有权限.. 以下是上传文件的示例代码..

if(isset($_POST['Submit']))
    {
        $tmp_name = $_FILES['Pic']['tmp_name'];
        $name = $_FILES['Pic']['name']; 

        if(is_uploaded_file($tmp_name))
        {
            $org_name = time()."_".$name;
            $dest = "uploads/$org_name";
            move_uploaded_file($tmp_name, $dest);
        }
    }

答案 2 :(得分:0)

您的问题可以有多个答案如下:

  1. 使用DOT(。)启动路径,例如'./opt/foldername /'
  2. 创建文件夹并为其提供您要插入文件和数据的写入权限
  3. 或者你可以做之前上传的方式,如果不是使用mkdir函数而不是使用chmod给出写入权限而不是使用上传功能,首先检查目录是否存在。检查目录的存在总是一种更好的做法,而不仅仅是在那里移动文件。

答案 3 :(得分:0)

move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/xampp/nf/uploads/'. $_FILES['file']['name'])

@Peter Darmis