我的代码存在问题。我不知道我的代码有什么问题,因为当我测试并运行它到PHP版本5.4时,这是有效的。
然后,在我使用PHP版本5.6传输文件之后,这不再适用了。
请帮忙。我是新来的。
以下是从表单上传图片的代码
if(!empty($_FILES["cover"]["name"])){
$ext = explode(".",$_FILES["cover"]["name"]);
$filename = substr(md5(rand(5,1000).$_FILES["cover"]["name"]),5,15).'.'.$ext[1];
$uploadOk = 1;
$imageFileType = pathinfo($filename,PATHINFO_EXTENSION);
// Check file size
if ($_FILES["cover"]["size"] > 2621440 /*2.5mb*/) {
echo "<script>alert('Sorry, your file is too large. Be sure to optimize the image you want to upload.');</script>";
echo '<meta http-equiv="refresh" content="0; url='.$BASE_URL.'new.php?error=file"/>';
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');</script>";
echo '<meta http-equiv="refresh" content="0; url='.$BASE_URL.'new.php?error=file"/>';
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<script>alert('Sorry, your file was not uploaded.');</script>";
echo '<meta http-equiv="refresh" content="0; url='.$BASE_URL.'new.php?error=file"/>';
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["cover"]["tmp_name"],"uploads/".$filename)) {
echo '<meta http-equiv="refresh" content="0; url='.$BASE_URL.'edit.php?id='.$bookid.'"/>';
} else {
echo "<script>alert('Sorry, there was an error uploading your file.');</script>";
echo '<meta http-equiv="refresh" content="0; url='.$BASE_URL.'new.php?error=file"/>';
}
}
}
非常感谢任何建议和意见。感谢。