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!";
}
?>
以上是上传图片的代码?
这是正确的代码吗?
答案 0 :(得分:1)
从输入中上传图片并使用#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;
}