我正在尝试让文件上传元素在我们的网页上工作,这样客户就可以上传图片给我们了。我从code pen下载了一个漂亮的图片上传元素
然后我从w3学校得到了PHP代码(完整的代码在底部):
https://www.w3schools.com/php/php_file_upload.asp
浏览器在第一次检查“getimagesize”时返回错误。上面写着:
警告:image.Sorry大小():文件名在第10行的W:\xampp\htdocs\image-upload-preview\upload.php中不能为空文件不是image.Sorry,您的文件未上载。
<form action="upload.php" method="post" enctype="multipart/form-data">
<h1>Image-upload with preview</h1>
<div class="wrapper">
<div class="box">
<div class="js--image-preview"></div>
<div class="upload-options">
<label>
<input type="file" class="image-upload" accept="image/*" name="fileToUpload" />
</label>
</div>
</div>
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
发布于 2018-08-16 03:46:02
你得到的警告并没有破坏你的代码。要修复警告,请检查您调用的文件是否已设置为
$checkfile = isset($_FILES['fileToUpload']['tmp_name']) ? $_FILES['fileToUpload']['tmp_name'] : '';
然后
$check = getimagesize($checkfile);
您遇到的问题是文件是空的,所以它正在运行else语句并使$uploadOk = 0。尝试找出图像的位置,并确保将其放入$_FILES的‘fileToUpload数组中。
发布于 2018-08-17 00:25:51
感谢您的回复。我从头开始编写了一个更好的教程here我还必须解决php,ini中的文件大小限制(然后忘记重启apache),但我最终让它工作了。下面是我的php代码:
<?php
if (isset($_POST['submit'])){
//the name 'file' is from the HTML
//$_FILES is a super global
$file = $_FILES['file'];
//creating variables for the file attributes
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
//creates an array for the name and extension
$fileExt = explode('.', $fileName);
//creates variable for the lower case extension, from the end of the array
$fileActualExt = strtolower(end($fileExt));
//allowed extensions
$allowed = array('jpg','jpeg','png','tif');
//checks
if(in_array($fileActualExt,$allowed)){
if($fileError === 0){
if($fileSize < 25000000){
//creates a new file name
$fileNameNew = uniqid('',true).".".$fileActualExt;
//set file destination
$fileDestination = 'uploads/'.$fileNameNew;
//moves from temp to proper destination
move_uploaded_file($fileTmpName,$fileDestination);
//goes back to previous page upon sucess
header("Location: index.html?uploadsucsess");
}else{
echo "File must be less than 25 mb";
}
} else{
echo "There was an error uploading your file";
echo $fileError;
}
} else{
echo "Invalid File Type Must be a JPG, PNG, or TIF";
}
}
https://stackoverflow.com/questions/51864604
复制相似问题