PHP - 上传文件似乎无法正常工作

时间:2014-12-03 07:44:17

标签: php

这里对PHP很新。我复制了一个在线脚本来尝试进行文件上传,但脚本仍然显示消息“无法复制文件”。我在脚本的文件夹中创建了文件夹“Uploads”,我检查了允许文件上传的php.ini文件。

如何解决此问题?

<?php
    if( $_FILES['file']['name'] != "" )
{
    copy( $_FILES['file']['name'], "Uploads/" ) or 
       die( "Could not copy file!");
}
    else
 {
      die("No file specified!");
 }
?>

注意:遵循本教程:http://www.tutorialspoint.com/php/php_file_uploading.htm

4 个答案:

答案 0 :(得分:1)

这是关于如何上传文件的示例。

if(is_uploaded_file($_FILES['file']['tmp_name'])){ 
    $folder = "Uploads/"; 
    $file = basename( $_FILES['file']['name']); 
    $full_path = $folder.$file; 
    if(move_uploaded_file($_FILES['file']['tmp_name'], $full_path)) { 
        echo "succesful upload, we have an image!";
    } else { 
       echo "upload received! but process failed";
    } 
}else{ 
    echo "upload failure ! Nothing was uploaded";
} 

使用move_uploaded_file()

答案 1 :(得分:1)

HTML部分

    <form action="" name="entryform" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><input type="submit" name="submit" value="Upload">
</form>

PHP部分

if ($_FILES["file"]["error"] > 0)
  {
  //echo "Error: " . $_FILES['file']['error'] . "<br />";
      echo "<script>alert('Please Select File'); 
                location = 'upload.php';
                </script>";
                exit();
  }
else
  {
 if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
            echo "<script>alert('File Already Exists'); 
                location = 'upload.php';
                </script>";
                exit();
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
                  echo "<script>alert('File Uploaded Successfully'); 
                    location = 'upload.php';
                </script>";
                exit();
      }
  }

答案 2 :(得分:0)

在php中使用 move_uploaded_file

move_uploaded_file (  $filename , $destination )

参考此manual

答案 3 :(得分:0)

更正此行副本($ _FILES [&#39; file&#39;] [&#39; name&#39;],&#34; Uploads /&#34;)like copy($ _FILES [&## 39;文件&#39;] [&#39; tmp_name&#39;],&#34;上传/&#34;)