我尝试使用表单通过PHP上传文件。这是我用来开始上传的PHP:
if(isset($_FILES['image'])){
$target_dir = "/var/www/html/uploads/";
$target_file = $target_dir . basename($_FILES["image"]["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["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
}
//if there is an error...
else
{
//set that to be the returned message
echo "Ooops! Your upload triggered the following error";
mysqli_stmt_close($stmt);
}
没有错误/通知
error_reporting(E_ALL);
ini_set('display_errors','On');
或在apache2错误日志中。 / var / www / html / uploads中文件夹的权限是777(仅用于开发),因此它是可写的。
答案 0 :(得分:1)
这是在目录中上传文件的方式在ht docs中应该有一个名为“UPLOAD”的文件夹,以查看上传的文件。
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"upload/".$file_name);
echo "Success";
}
else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
答案 1 :(得分:0)
您应该将上传的文件移动到$ target_dir。 使用move_uploaded_file($ _ FILES [“image”] [“tmp_name”],$ target_dir。“new_file_name”)