有些图片没有通过我的PHP代码上传到网站

时间:2017-03-09 18:13:29

标签: php html image-upload

所以我正在尝试上传图片,我收到了一些图片,这些图片主要是我的手机相机拍摄的图片或截图通过"剪切工具"给出的错误是"错误:上传文件时出错。请再试一次。"任何想法我的代码有什么问题?



if ($_POST && !empty($_FILES))
				{
					$formOk = true;

					//Assign Variables
					$path = $_FILES['image']['tmp_name'];
					$name = $_FILES['image']['name'];
					$size = $_FILES['image']['size'];
					$type = $_FILES['image']['type'];

					$conn=mysqli_connect("localhost","root","alaa","registration_database");
					$sql = "select max(id) as maximum from ads";
					if ($conn->connect_error) {
						die("Connection failed: " . $conn->connect_error);
					 
						}
					$result = $conn->query($sql);
					
					
					$row = $result->fetch_assoc();
					$id = $row['maximum'];
					$id++;
					$parts = explode(".", $name);
					$targetFile = "uploads/".$id.".".$parts[sizeof($parts)-1];
					
					
					if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
						$formOk = false;
						echo "Error: Error in uploading file. Please try again.";
					}

					//check file extension
					if ($formOk && !in_array($type, array('image/png','image/PNG', 'image/x-png', 'image/jpeg','image/JPG', 'image/pjpeg', 'image/gif'))) {
						$formOk = false;
						echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
					}
					// check for file size.
					if ($formOk && filesize($path) > 2097152) {
						$formOk = false;
						echo "Error: File size must be less than 2 MB.";
					}

					if ($formOk) {
						// read file contents
						//$targetFile = file_get_contents($path);
						if(move_uploaded_file($path,$targetFile)){
							echo "image uploaded";
						} else {
							echo "Error while uploading";
						}
					}
				}




0 个答案:

没有答案