使用php将文件上载到服务器
很好,很高兴见到你 我有一个与Angula,Laravel和Bootstrap的项目,我正在创建一个表格,以便能够通过项目的网站上传文件到服务器,我已经把这个代码放在下面,它不能正常工作,你能帮我告诉我我必须放的代码,或者告诉我哪里失败了 等待您的回答,接受诚挚的问候 多谢各位 这是在我的HTML中使用php将文件上载到服务器,php,angular,laravel,file,upload,Php,Angular,Laravel,File,Upload,很好,很高兴见到你 我有一个与Angula,Laravel和Bootstrap的项目,我正在创建一个表格,以便能够通过项目的网站上传文件到服务器,我已经把这个代码放在下面,它不能正常工作,你能帮我告诉我我必须放的代码,或者告诉我哪里失败了 等待您的回答,接受诚挚的问候 多谢各位 这是在我的HTML中 <div class="container"> <div class="row"> <form method=&qu
<div class="container">
<div class="row">
<form method="post" action="test_act.php" enctype="multipart/form-data">
<div>
<span>Choose file</span>
<input type="file" name="fileToUpload" id="fileToUpload"><br/>
<input type="submit" name="fupload" id="fupload">
</div>
</form>
</div>
</div>
选择文件
test_act.php
<?php
include ("test_cls.php");
if(isset($_POST['fupload']))
{
$dirpath="upload";
$uploader = new Uploader();
$uploader->setExtensions(array('jpg','jpeg','png','gif','doc','docx', 'pdf')); //allowed extensions list//
$uploader->setMaxSize(1); //set max file size to be allowed in MB//
$uploader->setDir($dirpath);
if($uploader->uploadFile('fileToUpload')) //txtFile is the filebrowse element name //
{
$suc_file = $uploader->getUploadName(); //get uploaded file name, renames on upload//
echo $suc_file." successfully uploaded";
}
else //upload failed
echo $uploader->getMessage(); //get upload error message
}
?>
test_cls.php(上传类)
您的代码运行正常 但是,您可以检查:
- 是否创建了上载目录
- 上载目录是否具有所需的权限
- 您的文件未超过php.ini定义的上载限制大小
<?php
class Uploader
{
private $destinationPath;
private $errorMessage;
private $extensions;
private $maxSize;
private $uploadName;
public $name='Uploader';
function setDir($path){
$this->destinationPath = $path;
}
function setMaxSize($sizeMB){
$this->maxSize = $sizeMB * (1024*1024);
}
function setExtensions($options){
$this->extensions = $options;
}
function setMessage($message){
$this->errorMessage = $message;
}
function getMessage(){
return $this->errorMessage;
}
function getUploadName(){
return $this->uploadName;
}
function uploadFile($fileBrowse){
$result = false;
$size = $_FILES[$fileBrowse]["size"];
$name = $_FILES[$fileBrowse]["name"];
$ext = pathinfo($name,PATHINFO_EXTENSION);
$this->uploadName= $name;
if(empty($name))
{
$this->setMessage("File not selected ");
}
else if($size>$this->maxSize)
{
$this->setMessage("Too large file !");
}
else if(in_array($ext,$this->extensions))
{
if(!is_dir($this->destinationPath))
mkdir($this->destinationPath);
if(!is_dir($this->destinationPath.'/'.$ext))
mkdir($this->destinationPath.'/'.$ext);
if(file_exists($this->destinationPath.'/'.$ext.'/'.$this->uploadName))
$this->setMessage("File already exists. !");
else if(!is_writable($this->destinationPath.'/'.$ext))
$this->setMessage("Destination is not writable !");
else
{
if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.'/'.$ext.'/'.$this->uploadName))
{
$result = true;
}
else
{
$this->setMessage("Upload failed , try later !");
}
}
}
else
{
$this->setMessage("Invalid file format !");
}
return $result;
}
function deleteUploaded($fileBrowse){
$name = $_FILES[$fileBrowse]["name"];
$ext = pathinfo($name,PATHINFO_EXTENSION);
unlink($this->destinationPath.'/'.$ext.'/'.$this->uploadName);
}
}
?>