我正在开发一个可以上传图片的网页,但代码似乎不起作用。这是另存为 profile2.php 的表单代码:
<form enctype="multipart/form-data" action="upload2.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
现在这里是upload2.php 代码:
<?php
$target = "pictures/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$uploaded = basename( $_FILES['uploaded']['name']) ;
//This is our size condition
if ($uploaded_size > 1) {
echo "Your file is too large.<br>";
die();
}
// This is our limit file type condition
echo $uploaded_type;
if (!($uploaded_type=="image/png")&&!($uploaded_type=="image/jpg")) {
echo "You may only upload png or jpg files.<br>";
die();
}
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". $uploaded ." has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
?>
所以首先。当我尝试上传 png 图片时,它读取了第二个 if 语句,并说只允许使用 png 和 jpg 文件。我期待它读取第一个 if 语句,因为我的文件肯定大于 1KB。我不知道为什么它忽略了第一个 if 语句并做了第二个 if 语句。任何人都可以帮我解决我的上传代码吗?