我想创建一个PHP代码,允许我将照片上传到我的网站。问题是我第一次单击上载按钮,消息是“成功”,但没有文件进入我的文件夹,第二次我没有更改代码,这引发了我和错误消息。你能看看我是否做错了吗?
<?php
if(isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg','png');
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
if($fileSize < 2048000){
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/images'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: profile.html?upload=succes");
} else {
echo "Your file is too big!";
}
} else {
echo " There was an error uploading your file!";
}
}else {
echo "You cannot upload files of this type!";
}
}
我收到错误消息“上传文件时出错”。
HTML表单
<form action="upload.php" method="POST" enctype="multipart/form-data">
<span> Change Profile Picture</span>
<input class="btn btn-default btn-rounded mb-4" type="file" name="file">
<button class="btn btn-default btn-rounded mb-4" type="submit" name="submit">Upload</button>
</form>
当我点击print_r($ file); 它使我感到困惑([name] => IMG_6389.jpg [type] => [tmp_name] => [error] => 1 [size] => 0)
`我得到错误1而不是0
答案 0 :(得分:0)
这是文件大小的问题。您必须在php.ini中增加上传文件的最大大小。
错误1为UPLOAD_ERR_INI_SIZE:
上传的文件超出了中的upload_max_filesize指令 php.ini。