我目前正在编写最基本的文件上传代码,以便从输入的type=" file“属性转到我们的服务器。这是我的HTML:
<form enctype="multipart/form-data" action="register-complete.php" method="post">
<h5>Register Now</h5>
<input type="text" class="form-control" name="Username" placeholder="Login Name"/><br />
<input type="text" class="form-control" name="Displayname" placeholder="Display Name"/><br />
<input type="text" class="form-control" name="Email" placeholder="Email" /><br />
<input type="radio" name="Paypal" value="1" /> This is my Paypal email.<br />
<input type="radio" name="Paypal" value="0" /> I do not want payment. I wish to preserve anonymity.<br /><br />
Avatar Picture: <br /><input type="file" name="AvatarPicture" id="AvatarPicture" />*500kb max file size.<br />*Accepted filetypes: .jpg, .png<br /><br />
<input type="text" class="form-control" name="Description" placeholder="Account Description"/><br />
<input type="password" class="form-control" name="Password" placeholder="Password"/><br />
<input type="password" class="form-control" name="PasswordConfirm" placeholder="Confirm Password"/><br />
<p class="text-center"><input type="submit" class="btn btn-primary" value="Register" name="submit" /></p>
</form>基本上,我只是关心AvatarPicture输入,然后将它上传到我的服务器上。下面是PHP代码,我必须这样做。
$username = $_REQUEST["Username"];
$displayname = $_REQUEST["Displayname"];
$email = $_REQUEST["Email"];
$paypal = $_REQUEST["Paypal"];
$target_dir = "images/avipictures/";
$target_file = $target_dir . basename($_FILES["AvatarPicture"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["AvatarPicture"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["AvatarPicture"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Sorry, only JPG, JPEG, PNG files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["AvatarPicture"]["tmp_name"],$target_file)) {
echo "The file has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}这与tee的w3schools代码完全相同。但是,除非我将move_uploaded_file()中的“move_uploaded_file”更改为"name",否则将收到一个内部服务器500错误,这只会导致else语句。我一整天都在搞这个,我一直在为这段代码应该有多简单而大发雷霆,但看起来一点也不简单。有什么想法吗?(此外,file_uploads设置为on,默认的最大文件大小设置为50 is。)
发布于 2015-10-28 19:39:55
如果没有一个像样的日志文件,我就无法判断问题出在哪里,但可能是您试图写入的目录不存在,或者您没有写入它的权限。您是否确定要写入的图像/导航图片/目录存在,并且and服务器具有写入它的权限?如果没有,则需要创建目录并设置对其的权限,以便set服务器可以写入目录。
https://stackoverflow.com/questions/33399418
复制相似问题