【问题标题】:Upload file to a server using php使用php将文件上传到服务器
【发布时间】:2021-02-03 02:24:12
【问题描述】:

很好,很高兴见到你,

我有一个使用 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
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);
    }
}
?>

【问题讨论】:

    标签: php angular laravel file upload


    【解决方案1】:

    您的代码运行良好。

    但是,您可以检查:

    • 是否已创建上传目录
    • 上传目录是否具有所需的权限
    • 您的文件未超过 php.ini 定义的上传限制大小

    【讨论】:

    • 您好,很高兴与您打招呼,感谢您的快速响应,我已经测试了权限是好的,php.ini 是 10MB,我知道它是正确的,默认情况下我只有在服务器上创建了上传文件夹,我在哪里必须在服务器上创建上传文件夹?等待您的回复,收到亲切的问候,非常感谢,
    • 嗨。我的荣幸。这不是因为当前用户拥有 www-data 用户(如果您使用 Apahce)所拥有的所需权限。当心 !您的开发环境是什么(PHP 版本、操作系统、...)?
    • 嘿@Aaron_Actu 我试图调试提交按钮,我注意到它没有执行任何代码。我不知道为什么,我会尝试找到一个解决方案,但如果你能帮我一把,那将有很大的帮助。
    • 很奇怪。可以发一张代码所在目录的图片吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多