Javascript 无法将图像上载到服务器

Javascript 无法将图像上载到服务器,javascript,php,html,Javascript,Php,Html,下面是我的php脚本,用于将图像上传到服务器 但我得到了错误:无效的文件大小或类型 如何解决此问题 <?php error_reporting(E_ALL); ini_set('display_errors', 1); if (isset($_POST['submit']) $j = 0; // Variable for indexing uploaded image. $target_path = "wep/"; // Declaring Path for uploaded image

下面是我的php脚本,用于将图像上传到服务器

但我得到了错误:无效的文件大小或类型

如何解决此问题

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_POST['submit']) $j = 0; // Variable for indexing uploaded image.
$target_path = "wep/"; // Declaring Path for uploaded images.

for ($i = 0; $i < count($_FILES['file']['name']); $i++)
    {

    // Loop to get individual element from the array

    $validextensions = array(
        "jpeg",
        "jpg",
        "png"
    ); // Extensions which are allowed.
    $ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
    $file_extension = end($ext); // Store extensions in the variable.
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
    $j = $j + 1; // Increment the number of uploaded images according to the files in array.
    if (($_FILES["file"]["size"][$i] < 100000) // Approx. 100kb files can be uploaded.
     && in_array($file_extension, $validextensions))
        {
        if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path))
            {

            // If file moved to uploads folder.

            echo $j . ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            }
          else
            { //  If File Was Not Moved.
            echo $j . ').<span id="error">please try again!.</span><br/><br/>';
            }
        }
      else
        { //   If File Size And File Type Was Incorrect.
        echo $j . ').<span id="error">Invalid file Size or Type</span><br/><br/>';
        }
    }
}

?>

它对我有用你也可以试试这个-

    if(isset($_POST["submit"]))
    {
    $target_dir = "uploads/";
    $target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
    $uploadOk=1;

    // Check if file already exists
    if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($_FILES['uploadFile']['size'] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    // Only GIF files allowed 
    if (!($_FILES['uploadFile']['type'] == "image/gif" || "image/jpg" || "image/png"))
    {
        echo "Sorry, only GIF files are allowed.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } 
    else { 
        if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir))
        {
            echo "";        
        }

        else
        {
        echo "<h2 id='action' align='center'>There is an error with the Photo!</h2>";
        }
    }
}
if(设置($\u POST[“提交”]))
{
$target_dir=“uploads/”;
$target_dir=$target_dir.basename($_FILES[“uploadFile”][“name”]);
$uploadOk=1;
//检查文件是否已经存在
如果(文件存在($target\u dir.$\u FILES[“uploadFile”][“name”])){
echo“抱歉,文件已存在。”;
$uploadOk=0;
}
//检查文件大小
如果($_文件['uploadFile']['size']>500000){
echo“对不起,您的文件太大了。”;
$uploadOk=0;
}
//只允许使用GIF文件
如果(!($_FILES['uploadFile']['type']==“image/gif”| | |“image/jpg”| |“image/png”))
{
echo“对不起,只允许使用GIF文件。”;
$uploadOk=0;
}
//检查$uploadOk是否被错误设置为0
如果($uploadOk==0){
echo“抱歉,您的文件未上载。”;
//如果一切正常,请尝试上载文件
} 
否则{
如果(移动上传的文件($上传文件[“上传文件”][“tmp\U名称”],$target\U dir))
{
回声“;
}
其他的
{
echo“照片有错误!”;
}
}
}

然后在数据库列中保存
$target\u dir

文件的大小限制为100KB。可能你的形象太大了

固定样式,重新格式化代码