无法将图像上传到服务器

时间:2015-04-24 11:55:14

标签: 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 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/>';
        }
    }
}

?>

2 个答案:

答案 0 :(得分:0)

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

    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>";
        }
    }
}

然后在您的数据库列中保存$target_dir

答案 1 :(得分:0)

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

相关问题