2013-04-26 106 views
0

我是新的精简版的PHP。我试图上传一个简单的东西,但它不工作..帮助找出我在w3school中提到..我有一个代码..并且我创建了一个文件夹,称为上传..为什么它不工作..文件上传脚本抛出错误:“无效的文件”

<html> 
<body> 

<form action="upload_file.php" method="post" 
enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
    </form> 

    </body> 
</html> 

upload_file.php

 <?php 
     $allowedExts = array("gif", "jpeg", "jpg", "png"); 
      $extension = end(explode(".", $_FILES["file"]["name"])); 
     if ((($_FILES["file"]["type"] == "image/gif") 
     || ($_FILES["file"]["type"] == "image/jpeg") 
     || ($_FILES["file"]["type"] == "image/jpg") 
     || ($_FILES["file"]["type"] == "image/pjpeg") 
     || ($_FILES["file"]["type"] == "image/x-png") 
     || ($_FILES["file"]["type"] == "image/png")) 
     && ($_FILES["file"]["size"] < 20000) 
     && in_array($extension, $allowedExts)) 
     { 
     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 
     { 
     echo "Invalid file"; 
     } 
    ?> 
+1

更具体的几乎肯定会有所帮助。如同正确格式化您的代码。 – str 2013-04-26 14:59:42

+0

你的意思是什么不起作用? PHP代码是否完全不能运行?它会做与你所期望的不同的事情吗?有错误消息吗? – andrewsi 2013-04-26 15:00:13

+0

你的第一个错误是使用w3fools。他们不是一个有用的资源。他们的代码完全是垃圾,充满了安全漏洞和糟糕的做法。你的问题也需要工作。 “不工作”是** NOT **有用的信息。如果它有效,你不会在这里。所以告诉我们**如何**它不起作用。 – 2013-04-26 15:01:24

回答

1

增加文件大小,并添加允许大写扩展。肯定有更好的方法来做到这一点,但它的工作原理。我将20000增加到20000000

<?php 
    $allowedExts = array("gif", "GIF", "jpeg", "JPEG", "jpg", "JPG", "png", "PNG"); 
     $extension = end(explode(".", $_FILES["file"]["name"])); 
    if ((($_FILES["file"]["type"] == "image/gif") 
    || ($_FILES["file"]["type"] == "image/jpeg") 
    || ($_FILES["file"]["type"] == "image/jpg") 
    || ($_FILES["file"]["type"] == "image/pjpeg") 
    || ($_FILES["file"]["type"] == "image/x-png") 
    || ($_FILES["file"]["type"] == "image/png")) 
    && ($_FILES["file"]["size"] < 20000000) // increased allowed size may be your problem 
    && in_array($extension, $allowedExts)) 
    { 
    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 
    { 
    echo "Invalid file"; 
    } 
?> 
+0

非常感谢弗雷德......它的工作原理:) – Harini 2013-04-26 15:28:05

+0

@Harini不客气。享受学习PHP,欢呼;-)添加注意:看看自动将扩展名转换为小写,你会喜欢的。 – 2013-04-26 15:30:30

+0

我要如何在db中存储路径 – Harini 2013-04-26 16:11:16