Unable to upload video from flutter to server php?

2,844

OK, finally, I found solution. That is to increase upload limit in php.ini. I can upload images but not videos because video files are larger than 2MB of max upload limit.

Share:
2,844
ThitSarNL
Author by

ThitSarNL

Updated on December 12, 2022

Comments

  • ThitSarNL
    ThitSarNL over 1 year

    I tried to upload a video. Got the message "Video uploaded" and NO video file received in 'uploads' folder. When I tried to upload an image file, it works (uploaded image file has arrived in the 'uploads' folder). My code doesn't work with the video file. How to make it receive a video file?

    This is a dart file.

    import 'dart:async';
    import 'dart:convert';
    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:http/http.dart';
    import 'package:image_picker/image_picker.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Upload MySQL',
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    
      File _video;
    
      Future getVideoGallery() async{
        var imageFile = await ImagePicker.pickVideo(source: ImageSource.gallery);
    
        setState(() {
          _video = imageFile;
        });
      }
    
      Future getVideoCamera() async{
        var imageFile = await ImagePicker.pickVideo(source: ImageSource.camera);
                setState(() {
          _video = imageFile;
        });
      }
    
      Future uploadVideo(File videoFile) async{
        var uri = Uri.parse("http://192.168.1.1/ABC/uploadVideo.php");
        var request = new MultipartRequest("POST", uri);
    
        var multipartFile = await MultipartFile.fromPath("video", videoFile.path);
        request.files.add(multipartFile);
        StreamedResponse response = await request.send();
        response.stream.transform(utf8.decoder).listen((value) {
          print(value);
        });
        if(response.statusCode==200){
          print("Video uploaded");
        }else{
          print("Video upload failed");
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("Upload Image"),),
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
               _video==null
                  ? new Text("No video selected!")
                  : new Text("video is selected"),
                Row(
                  children: <Widget>[
                    RaisedButton(
                      child: Icon(Icons.video_library),
                      onPressed: getVideoGallery,
                    ),
                    RaisedButton(
                      child: Icon(Icons.videocam),
                      onPressed: getVideoCamera,
                    ),
                    RaisedButton(
                      child: Text("UPLOAD video"),
                      onPressed:(){
                        uploadVideo(_video);
                      },
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      }
    }
    

    This is php file.

    <?php
    
    include 'conn.php';
    
    $video=$_FILES['video']['name'];
    $videoPath="uploads/".$video;
    move_uploaded_file($_FILES['video']['tmp_name'],$videoPath);
    
    ?>
    
  • OMi Shah
    OMi Shah almost 3 years
    Also note that unless server allows large file size uploads, nothing can work.
  • Khaled Mahmoud
    Khaled Mahmoud almost 3 years
    That's right but my problem was not with the server, it was the HTTP package.