【发布时间】:2015-01-09 13:21:41
【问题描述】:
如何在上传前获得以像素为单位的图像分辨率?有什么我想要的解决方案吗?无需更改此代码的大更改?请! 代码仅限制文件大小。但我需要限制文件大小和分辨率(以像素为单位)!
对不起!我的英语知识太差了。我希望你能理解这一点。谢谢!
<?php
if (isset($_POST['submit'])) {
$j = 0; //Variable for indexing uploaded image
$target_path = "uploads/"; //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", "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] < 500000) //Approx. 500kb 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/>';
}
}
}
?>
【问题讨论】:
-
您是否尝试使用 javascript 获取图像大小?
-
在这段PHP代码可以执行的时候,图片已经上传了
-
Soooo.. 你需要像素大小还是文件大小?标题说像素,但你稍后会谈论文件大小
-
@SamDufel 不,我的意思是有大小限制。我需要这样的php somthing。谢谢!
-
我真的不明白你想要什么。你谈论像素、分辨率和大小。但无论如何,你不能。在用户不上传文件之前,该文件位于客户端,您无权访问。
标签: javascript php html ajax image-upload