HTML:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF 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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
此文件上传非常适合我。但我想要更多: 如果有人上传文件,我只会看到他上传的文件。 我看不到该文件作者的名字。
有可能做类似的事情吗?
答案 0 :(得分:0)
我有用于文件上传的代码,您可以将其与代码进行比较,以查看问题所在。
关于扩展名,您需要确保将正确的扩展名上传到数据库并上传到文件夹。
注意::在使用该代码之前,请确保已将其命名为文件夹(上传),并且在此文件夹中将其命名为另一个文件夹(头像),以便在出现以下情况时可以将其上传到服务器中的文件夹中您使用我发布的代码。
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="avatar">
</form>
if(isset($_FILES['avatar'])){
//If you want to see the array for file do this
//print_r($_FILES['avatar']);
// avatar array
$avatarName = $_FILES['avatar']['name'];
$avatarSize = $_FILES['avatar']['size'];
$avatarTmp = $_FILES['avatar']['tmp_name'];
$avatarType = $_FILES['avatar']['type'];
// list of allowed file typed to upload
$avatarAllowedExtension = array('jpeg', 'jpg', 'png', 'gif');
// get avatar extension
$tmp = explode('.', $avatarName);
$avatarExtension = strtolower(end($tmp));
if (!empty($avatarName) && !in_array($avatarExtension, $avatarAllowedExtension)) {
echo 'This extension is not allowed';
}
if (empty($avatarName)) {
echo 'Avatar is required';
}
if ($avatarSize > 4194304) {
echo 'Avatar cant be larger than 4mb';
}
//using rand(min,max) to insert in database different name
$avatar = rand(0, 100000000000) . '_' . $avatarName;
//to keep file insert folder if you want to show it or if you want to download it in your website
move_uploaded_file($avatarTmp, 'uploads\avatar\\' . $avatar);
//If you want to upload file use $avatar because you need different name in your database but when you show it to user you can show the original name for user file
}
答案 1 :(得分:-1)
如果系统未保存名称,则无法显示该名称。图形文件中不包含作者的姓名。