上传图片和文件


Upload image and file

enter code here
<?php
if (isset($_POST['btn_uplaodpic']))
{
    $id = $_POST['hf_uname'];
    move_uploaded_file($_FILES['file']['tmp_name'],"images/".$id.".jpg");
    echo "Successfully upload!";
}
?>

以上是上传图片的代码?

这是正确的代码吗?

从输入上传图像并使用#submit按钮发布。

   if (isset($_POST['submit'])) {
        $j = 0; //Variable for indexing uploaded image
        $target_path = "uploads/";
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png", "gif");  //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/>';
                printf("<script>location.href='drag.php'</script>.");
            } else {//if file was not moved.
                echo $j. ').<span id="error">Only JPG, JPEG, PNG and GIF files types allowed.</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/>';
        }
    }
}
?>

这将针对路径,验证文件类型以查看它是否是图像。这还会在上传时为每张图片分配一个随机的唯一 ID 名称。它将验证文件大小,如果不是图像或文件大小太大,则显示错误。

CSS的错误。

#noerror{
    color:green;
    font-weight:bolder;
    text-align: left;
}
#error{
    color:red;
    font-weight:bolder;
    text-align: left;
}