php 無法上傳附件
在PHP中使用內(nèi)置的`$_FILES`全局變量來獲取上傳文件的信息。當(dāng)你通過HTML表單提交個(gè)文件時(shí),這個(gè)文件就會(huì)被發(fā)送到服務(wù)器,且在這個(gè)變量中訪問。
```html
```
```php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = ;
} else {
echo "File is not an image.";
$uploadOk = ;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = ;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > ) {
echo "Sorry, your file is too large.";
$uploadOk = ;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = ;
}
// Check if $uploadOk is set to by an error
if ($uploadOk == ) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
```