【发布时间】:2016-01-13 11:50:58
【问题描述】:
我在尝试使此上传脚本正常工作时遇到问题。它不上传 .swf,即使脚本本身应该可以工作,我使用了图像上传器的脚本并对其进行了编辑,因此它应该可以与 .swf 文件一起使用,但 PHP 中似乎存在不允许的问题这些类型的文件要上传。
谁能帮我解决这个问题?
这是html
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select game (*.swf) to upload and add to the list of games:
<input type="file" name="gameToUpload" id="gameToUpload">
<input type="text" name="nameOfGame" id="nameOfGame">
<input type="submit" value="Upload Game" name="submit">
</form>
</body>
</html>
这就是 PHP
<?php
include('init.php');
//Reminder to self: Turn off when finished coding!!
error_reporting(-1);
//Some querys and variables used later on
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["gameToUpload"]["name"]);
$uploadOk = 1;
$gameFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$gameName = $_POST["nameOfGame"];
$query = "INSERT INTO `beoordelingen`.`beoordeling` (`ID`, `file`, `Spel`) VALUES (NULL, '{$target_file}', '{$gameName}')";
//If submit is pressed
if (isset($_POST['submit'])) {
$check = $_FILES["gameToUpload"]["tmp_name"];
//Check if file already exists in folder
if (file_exists($target_file)) {
echo "This game is already in the folder.";
$uploadOk = 0;
}
//Check if the file type is .SWF
if ($gameFileType != "swf") {
echo "Sorry, only .swf is allowed for this page.";
$uploadOk = 0;
}
//When any errors occured do not go passed this statement
if ($uploadOk === 0) {
echo "Sorry, your file wasn't uploaded.";
}
//If no errors have occured then continue with this
else {
//Move the file from the temporary folder to the final destination
if (move_uploaded_file($_FILES["gameToUpload"]["tmp_name"], $target_file)) {
echo "The file " . basename($_FILES["gameToUpload"]["name"]). " has been uploaded.";
//Update the mysql database
$conn->query($query);
}
//If something bad happened while trying to move the file. Show this message to the user.
else {
echo "Sorry, there was an error uploading the file.";
}
}
}
?>
【问题讨论】:
-
你得到什么错误?
-
可能是因为这种情况:
if ($gameFileType != "swf") { ... } -
@Dennis1679 假设您正在尝试上传文件 MyGame.SWF 。文件扩展名将是“SWF”而不是“swf”(记住字符串在 php 中区分大小写)。比较两个字符串的正确方法是:
if( strcasecmp($gameFileType, "swf") === 0 ) { /* Is a swf file */ } -
@CarlosAlbertoB.Carucce 好的,这可能是未来的问题,所以谢谢。但在这种情况下,我上传的文件具有小写扩展名。所以这不可能是我的问题的原因。我会改变它,但只是为了确保。
-
@Dennis1679 $target_dir 也可能不是有效的相对文件夹...您可以使用 ´$target_dir = _ DIR _."/uploads/"; 将其设置为绝对文件夹'.并且不要忘记检查它是否具有写入权限 P.S:(删除 DIR 常量中的空格。由于 S.Overflow 格式,我无法删除它)