【问题标题】:I try to upload a file, API Silex我尝试上传文件,API Silex
【发布时间】:2015-03-10 22:24:16
【问题描述】:

我使用 PHP/Silex 做一个 API。 我所有的代码工作都只是一条路...... 这是我的 API 代码的 index.php 有问题的地方:

require_once __DIR__.'/../vendor/autoload.php';  
header("Access-Control-Allow-Origin: *");
$app = new Silex\Application();

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\File\UploadedFile;


$app->post('/upload', function (Request $request) use ($app)
       {
         $file = $request->files->get('upload');                                                                     
         if ($file == NULL)   
       {
     $send = json_encode(array("status" => "Fail"));
     return $app->json($send, 500);
       }
     else
       {
     $file->move(__DIR__.'/../files', $file->getClientOriginalName());
     $send = json_encode(array("status" => "Ok"));
             return $app->json($send, 200);
       }
       });
$app->run();

当我评论这行$file->move(__DIR__.'/../files', $file->getClientOriginalName());时程序返回状态,但是如果我让这行。我有一个text/html 作为回应,我们可以在这里阅读,

..Whoops, looks like something went wrong
FileException in File.php line 134:
Unable to create the "/var/www/api-picShary/web/../files" directory
in File.php line 134
at File->getTargetFile('/var/www/api-picShary/web/../files',  'awesome.png') in UploadedFile.php line 239
at UploadedFile->move('/var/www/api-picShary/web/../files', 'awesome.png') in index.php line 79
at {closure}( object( Request))
at call_user_func_array( object( Closure), array( object( Request))) in HttpKernel.php line 145
at HttpKernel->handleRaw( object( Request), '1') in HttpKernel.php line 66
at HttpKernel->handle( object( Request), '1', true) in Application.php line 543
at Application->handle( object( Request)) in Application.php line 520

在 Application->run() in index.php 第 86 行...

我使用表格来发送我的文件:

< form action="" method="post" enctype="multipart/form-data">
< input type="file" name="upload">
< input type="submit">
< /form>

【问题讨论】:

  • 您确定网络服务器用户可以写入文件目录吗?您也可以尝试启用调试模式 ($app['debug'] = true;),您会收到更好的错误消息。
  • @mTorres 是的,他可以(确保 chmod 777 并 chown www-data)并且调试模式已启用,但没有任何改变......
  • @mTorres 编辑:我重新启动服务器,$app['debug'] 工作

标签: php html post apache2 silex


【解决方案1】:

问题有 php.ini ,我不得不更改 upload_max_filesize post_max_size

/etc/php5/cli/conf.d/php.ini

工作

结束

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-26
    • 2018-05-12
    • 2011-06-02
    • 2020-03-04
    • 2015-08-31
    • 2018-06-05
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多