Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将文件上载到服务器_Php_Angular_Laravel_File_Upload - Fatal编程技术网

使用php将文件上载到服务器

使用php将文件上载到服务器,php,angular,laravel,file,upload,Php,Angular,Laravel,File,Upload,很好,很高兴见到你 我有一个与Angula,Laravel和Bootstrap的项目,我正在创建一个表格,以便能够通过项目的网站上传文件到服务器,我已经把这个代码放在下面,它不能正常工作,你能帮我告诉我我必须放的代码,或者告诉我哪里失败了 等待您的回答,接受诚挚的问候 多谢各位 这是在我的HTML中 <div class="container"> <div class="row"> <form method=&qu

很好,很高兴见到你

我有一个与Angula,Laravel和Bootstrap的项目,我正在创建一个表格,以便能够通过项目的网站上传文件到服务器,我已经把这个代码放在下面,它不能正常工作,你能帮我告诉我我必须放的代码,或者告诉我哪里失败了

等待您的回答,接受诚挚的问候

多谢各位

这是在我的HTML中

<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.ini为10MB,我知道这是正确的,默认情况下,我只在服务器上创建上载文件夹,我必须在服务器上的何处创建上载文件夹?等待您的回复,收到诚挚的问候,非常感谢,您好。我的荣幸。这并不是因为当前用户拥有www数据用户(如果使用Apahce)所需的权限。小心!你的开发环境是什么(PHP版本,OS,…)?嘿@Aaron_Actu我试着调试submit按钮,我注意到它没有执行任何代码。我不知道为什么,我会设法找到一个解决办法,但如果你能帮我一把,那将是非常有帮助的。你能给我们发一张代码所在目录的照片吗?
<?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);
    }
}
?>