文件无法在PHP上传

时间:2014-04-03 06:34:08

标签: php html

我尝试使用html和PHP上传3个文件,在本地服务器上它正在运行但是当我托管它时,此代码无效。可能是什么问题?

这是我的HTML和PHP代码:

HTML CODE:

<form name="test" id="test" action="pet_up.php" enctype="multipart/form-data" method="POST">
  <input type="hidden" name="MAX_FILE_SIZE" value="5097152" /> 
   Upload File 1:<input type='file' id='f1' name='f1'/>
   Upload File 2:<input type='file' id='f2' name='f2'/>
   Upload File 3:<input type='file' id='f3' name='f3'/>
  <input type="submit" value="Update"/>
</form>

PHP代码:

if(isset($_FILES['f1']['name']))
{
$image1 = $_FILES['f1']['name']; 
$tmp1 = $_FILES['f1']['tmp_name']; 
$pathAndName1 = "uploads/".$image1;
if(!empty($_FILES) && file_exists($tmp1) && is_uploaded_file($tmp1)) 
{
    move_uploaded_file($tmp1,$pathAndName1);      
} 
else
{
    $pathAndName1="";
}
echo"<script>alert(".$pathAndName1.");</script>";
$qry="update petition set f1='".$pathAndName1."'";
$result=mysql_query($qry);
}


if(isset($_FILES['f2']['name']))
{
$image2 = $_FILES['f2']['name']; 
$tmp2 = $_FILES['f2']['tmp_name']; 
$pathAndName2 = "uploads/".$image2;
if(!empty($_FILES) && file_exists($tmp2) && is_uploaded_file($tmp2))
{
    move_uploaded_file($tmp2,$pathAndName2);      
} 
else
{
    $pathAndName2="";
}
echo"<script>alert(".$pathAndName2.");</script>";
$qry="update petition set f2='".$pathAndName2."'";
$result=mysql_query($qry);
}

if(isset($_FILES['f3']['name']))
{   
$image3 = $_FILES['f3']['name']; 
$tmp3 = $_FILES['f3']['tmp_name']; 
$pathAndName3 = "uploads/".$image3;
if(!empty($_FILES) && file_exists($tmp3) && is_uploaded_file($tmp3)) 
{
    move_uploaded_file($tmp3,$pathAndName3);      
} 
else
{
    $pathAndName3="";
}
echo"<script>alert(".$pathAndName3.");</script>";
$qry="update petition set f3='".$pathAndName3."'";
$result=mysql_query($qry);
}

2 个答案:

答案 0 :(得分:1)

默认情况下,您可以上传大小为2Mb的文件,如果您想要更大的文件上传,请更改您的php.ini文件并设置您要上传的值。

答案 1 :(得分:-1)

请使用此查询的简化版本..这将帮助您更轻松地进行调试......

$count = count($_FILES);
for ($i = 1; $i <= $count; $i++)
{
    if (isset($_FILES['f' . $i]['name']))
    {
        $image . $i= $_FILES['f' . $i]['name'];
        $tmp . $i= $_FILES['f' . $i]['tmp_name'];
        $pathAndName . $i= "uploads/" . $image . $i;
        if (!empty($_FILES) && file_exists($tmp . $count) && is_uploaded_file($tmp . $count))
        {
            move_uploaded_file($tmp . $i, $pathAndName . $i);
            echo 'error uploading file'.$i;
        }
        else
        {
            $pathAndName . $i= "";
        }
    }
    if ($pathAndName . $i!= '')
    {
        echo"<script>alert(" . $pathAndName . $i. ");</script>";
    }
    $qry = mysql_query("update petition set f" . $i. "='" . $pathAndName . $i. "'");
    if(!$qry)
    {
        echo 'error in query'.$i;
    }
}