【问题标题】:how to upload image to symfony php server如何将图像上传到 symfony php 服务器
【发布时间】:2016-08-14 22:45:00
【问题描述】:

我有这个有效的代码:

session_start();     
if ( isset($_SESSION['username']) ) {
    $params = explode(".", basename($_FILES["file"]["name"]));
    $target = array_values($params)[0];
    $id = array_values($params)[1];
    if ( ($target == "profile") || ($target == "question" && $_SESSION["waitingImagesQuestion"] > 0) || ($target == "answer" && $_SESSION["waitingImagesAnswer"] > 0) ) {
        $imageDirectory = "images/".round(microtime(true)*1000).".png";
        if ( move_uploaded_file($_FILES["file"]["tmp_name"], $imageDirectory) ) {
            print "success";

        } 
    }
} 
print JsonBuilder::getStringError();

现在我想要对 symfony 执行相同操作的代码。 怎么写?

谢谢

【问题讨论】:

    标签: php upload image-uploading symfony


    【解决方案1】:

    Symfony.com 上有一个食谱项目解释如何上传文件。看看http://symfony.com/doc/current/cookbook/controller/upload_file.html 和/或http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

    要获得像用户名一样的 Session 变量,您可以在控制器中使用 session 服务:

    $this->get('session')->get('username');
    

    【讨论】:

    • 我已经读过这些了!我需要一个和我一样的代码!因为我必须将图像保存到我的文件系统而不是 int DB!
    • 在第一个链接中有一个关于如何上传文件而不存储在数据库中的示例。无论如何,如果你不想使用实体,你可以从请求中获取文件($request->files->get('name')),它是UploadedFile 的一个实例。之后,您可以按照文档中的说明移动文件 ($file->move($directory, $fileName);)
    • 一个问题,名字是什么?这里'$request->files->get('name')'
    【解决方案2】:

    对于会话,您可以使用 $this->getUser()

    对于上传mabe,此链接很有帮助 http://symfony.com/doc/current/cookbook/controller/upload_file.html

    【讨论】:

      【解决方案3】:

      此代码有效!

      use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
      use Symfony\Bundle\FrameworkBundle\Controller\Controller;
      use Symfony\Component\HttpFoundation\Response;
      
      
      class SendImage extends Controller {
      
      
          /**
           * @Route("/apprequests/SendImage")
           */
          public function numberAction() {
              $jsonManager = $this->get('epb.JsonManager');
              $session = $this->get('session');
              if ( $session->isStarted() ) {            
                  $params = explode(".", basename($_FILES["file"]["name"]));
                  $target = array_values($params)[0];
                  $id = array_values($params)[1];
                  if ( ($target == "profile") || ($target == "question" && $session->get('waitingImagesQuestion') > 0) || ($target == "answer" && $session->get('waitingImagesAnswer') > 0) ) {
                      $imageName = round(microtime(true)*1000).".png";
                      $imageDirectory = "images/".$imageName;
                      if ( move_uploaded_file($_FILES["file"]["tmp_name"], $imageDirectory) ) {
                          print "success";
                      } 
                  }
              } 
              else {
                  return new Response($jsonManager->getStringError());
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2013-11-02
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 2019-03-04
        • 2014-02-13
        • 2011-02-02
        • 2015-03-29
        • 2011-10-15
        相关资源
        最近更新 更多