php oop文件上传 [英] Php oop file upload
问题描述
我正在处理oop php文件上传脚本.很简单.但是不起作用.问题是什么?我学习了如何使用$ _FILE以及如何编码oop风格.
I work on a oop php file upload script. Is simple. But doesnt work. What is the problem? I learn how to use $_FILE, and how to code oop style.
谢谢.
upload.php是:
upload.php are :
<?php
class upload{
public $src = "./upload/";
public $tmp;
public $filename;
public $type;
public $uploadfile;
public function startupload(){
$this -> filename = $_FILES["file"]["name"];
$this -> tmp = $_FILES["file"]["tmp_name"];
$this -> uploadfile = $src . basename($this -> name);
}
public function uploadfile(){
if(move_uploaded_file($this -> tmp, $this -> uploadFile)){
return true;
}
}
}
?>
index.php是:
index.php are:
<?php
require_once('./lib/upload.php');
?>
<?php
if(isset($_POST['file'])){
$fileupload = new upload();
if($fileupload -> uploadfile()){
echo 'Fisierul a fost uploadat';
}
}
?>
<html>
<head></head>
<body>
<form align="center" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Select upload file: <input type="file" name="file" required="yes" />
<input type="submit" value="Trimite" />
<p>
</form>
</body>
</html>
我在哪里弄错我的修饰语?
Where i'm wrong with my tinking?
推荐答案
未触发代码中的问题是因为您在post变量中检查了变量文件,却找不到该变量.
The problem in your code is not triggered is because you checked variable file in the post variable and you wont find it there. the corect way to do it is
if(isset($_FILES['file'])) {
$fileupload = new upload();
if($fileupload -> uploadfile()) {
echo 'Fisierul a fost uploadat';
}
}
另外,您的课程将无法正常工作,您应该将变量传递给构造函数,然后将upload-> startupload()重命名为upload-> upload
further more your class will not work you should pass the variables to a constructor and rename upload-> startupload() to upload-> upload
这篇关于php oop文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!