Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karimali1337's avatar

Move record with laravel move_uploaded_file(): Unable to move

I'm recording with script voice record and its saved to db but not moving to the public folder i've dd every single code and its working but don't know why the method of moving not working and when i dd($file); all the info and file type correct,method linked to api.php and route working correctly but cannot find out the problem in moving Error

Error

Symfony\Component\HttpFoundation\File\Exception\FileException: Could not move the file "C:\xampp\tmp\phpA7EA.tmp" to "records22-04-13T12:01:58.276Z.wav" (move_uploaded_file(): Unable to move "C:\xampp\tmp\phpA7EA.tmp" to "records22-04-13T12:01:58.276Z.wav"). in file C:\xampp\htdocs\blog\vendor\symfony\http-foundation\File\UploadedFile.php on line 191

Controller method

public function upload_record(Request $request)
    {

        if($request->file('recorder')){

                    $file=$request->file('recorder');


                  $name =$file->getClientOriginalName();
                  dd($name);

                  $file->move('records', $name);
        }
    }

The whole Script

 <script src="http://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>

    <script>
        //webkitURL is deprecated but nevertheless
        URL = window.URL || window.webkitURL;

        var gumStream; //stream from getUserMedia()
        var rec; //Recorder.js object
        var input; //MediaStreamAudioSourceNode we'll be recording

        // shim for AudioContext when it's not avb.
        var AudioContext = window.AudioContext || window.webkitAudioContext;
        console.log(AudioContext);
        var audioContext //audio context to help us record

        var recordButton = document.getElementById("recordButton");
        var stopButton = document.getElementById("stopButton");
        var pauseButton = document.getElementById("pauseButton");
        //add events to those 2 buttons
        recordButton.addEventListener("click", startRecording);
        stopButton.addEventListener("click", stopRecording);
        pauseButton.addEventListener("click", pauseRecording);

        function startRecording() {
            console.log("recordButton clicked");

            /*
             Simple constraints object, for more advanced audio features see
             https://addpipe.com/blog/audio-constraints-getusermedia/
             */

            var constraints = {
                audio: true,
                video: false
            }

            /*
             Disable the record button until we get a success or fail from getUserMedia()
             */

            recordButton.disabled = true;
            stopButton.disabled = false;
            pauseButton.disabled = false

            /*
             We're using the standard promise based getUserMedia()
             https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
             */

            navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
                console.log("getUserMedia() success, stream created, initializing Recorder.js ...");

                /*
                 create an audio context after getUserMedia is called
                 sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
                 the sampleRate defaults to the one set in your OS for your playback device

                 */
                audioContext = new AudioContext();

                //update the format
                document.getElementById("formats").innerHTML = "Format: 1 channel pcm @ " + audioContext
                    .sampleRate / 1000 + "kHz"

                /*  assign to gumStream for later use  */
                gumStream = stream;

                /* use the stream */
                input = audioContext.createMediaStreamSource(stream);

                /*
                 Create the Recorder object and configure to record mono sound (1 channel)
                 Recording 2 channels  will double the file size
                 */
                rec = new Recorder(input, {
                    numChannels: 1
                })

                //start the recording process
                rec.record()

                console.log("Recording started");

            }).catch(function(err) {
                //enable the record button if getUserMedia() fails
                recordButton.disabled = false;
                stopButton.disabled = true;
                pauseButton.disabled = true
            });
        }

        function pauseRecording() {
            console.log("pauseButton clicked rec.recording=", rec.recording);
            if (rec.recording) {
                //pause
                rec.stop();
                pauseButton.innerHTML = "Resume";
            } else {
                //resume
                rec.record()
                pauseButton.innerHTML = "Pause";

            }
        }

        function stopRecording() {
            console.log("stopButton clicked");

            //disable the stop button, enable the record too allow for new recordings
            stopButton.disabled = true;
            //false->true 1 recored
            recordButton.disabled = true;
            pauseButton.disabled = true;

            //reset button just in case the recording is stopped while paused
            pauseButton.innerHTML = "Pause";

            //tell the recorder to stop the recording
            rec.stop();

            //stop microphone access
            gumStream.getAudioTracks()[0].stop();

            //create the wav blob and pass it on to createDownloadLink
            rec.exportWAV(createDownloadLink);

        }

        function createDownloadLink(blob) {

            var url = URL.createObjectURL(blob);
            var au = document.createElement('audio');
            // au.setAttribute('name', 'recorder');
            var li = document.createElement('li');
            var link = document.createElement('a');
            //name of .wav file to use during upload and download (without extendion)
            var filename = new Date().toISOString();

            //add controls to the <audio> element
            au.controls = true;
            au.src = url;
            //save to disk link
            link.href = url;
            link.download = filename + ".wav"; //download forces the browser to donwload the file using the  filename
            // link.innerHTML = "Save to disk";

            //add the new audio element to li
            li.appendChild(au);

            //add the filename to the li
            li.appendChild(document.createTextNode(filename + ".wav "))

            //add the save to disk link to li
            li.appendChild(link);


            inputElemFile = document.createElement('input')
            inputElemFile.name = "recorder_file"
            inputElemFile.value = blob //string // file
            inputElemFile.type = 'hidden'
            inputElem = document.createElement('input')
            inputElem.name = "recorder"
            inputElem.value = filename + ".wav" //string // file
            inputElem.type = 'hidden'
            var inputDiv = document.getElementById('input-audio');
            inputDiv.append(inputElem);
            //inputDiv.append(inputElemFile);

            var xhr = new XMLHttpRequest();
            xhr.onload = function(e) {
                if (this.readyState === 4) {
                    console.log('save...!')
                }
            };

            var fd = new FormData();
            fd.append("recorder", blob, filename + '.wav');
            xhr.open("POST", "{{ route('upload.voice.record') }}", true);
            xhr.send(fd);




            //add the li element to the ol
            recordingsList.appendChild(li);
        }
    </script>

dd($file);

Illuminate\Http\UploadedFile {#1252
  -test: false
  -originalName: "2022-04-13T11:59:56.133Z.wav"
  -mimeType: "audio/wav"
  -error: 0
  #hashName: null
  path: "C:\xampp\tmp"
  filename: "phpCACD.tmp"
  basename: "phpCACD.tmp"
  pathname: "C:\xampp\tmp\phpCACD.tmp"
  extension: "tmp"
  realPath: "C:\xampp\tmp\phpCACD.tmp"
  aTime: 2022-04-13 11:59:56
  mTime: 2022-04-13 11:59:56
  cTime: 2022-04-13 11:59:56
  inode: 5629499534431248
  size: 41004
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\xampp\tmp\phpCACD.tmp"
}
0 likes
8 replies
karimali1337's avatar

@Sinnbeck

  public function upload_record(Request $request)
    {

        if($request->file('recorder')){

                    $file=$request->file('recorder');
                $path=$request->file->storeAs('records',$file);
                //   $file->move('records', $name);
        }
    }

give null error

Error: Call to a member function storeAs() on null in file C:\xampp\htdocs\blog\app\Http\Controllers\Admin\HomeController.php on line 19
Graham69's avatar

It is better to use move function for upload the file to Public folder. Then it will be easier for you to access the file from anywhere in your application.

Sinnbeck's avatar

@karimali1337 You need to check if there is a file

  public function upload_record(Request $request)
    {

        if($request->hasFile('recorder')){//check if it is in fact a file..

                    $file=$request->file('recorder');
                $path=$request->file->storeAs('records',$file);
                //   $file->move('records', $name);
        }
    }
karimali1337's avatar

@Sinnbeck i've dd($file);

and it returns that its type is file

Illuminate\Http\UploadedFile {#1252
  -test: false
  -originalName: "2022-04-13T11:59:56.133Z.wav"
  -mimeType: "audio/wav"
  -error: 0
  #hashName: null
  path: "C:\xampp\tmp"
  filename: "phpCACD.tmp"
  basename: "phpCACD.tmp"
  pathname: "C:\xampp\tmp\phpCACD.tmp"
  extension: "tmp"
  realPath: "C:\xampp\tmp\phpCACD.tmp"
  aTime: 2022-04-13 11:59:56
  mTime: 2022-04-13 11:59:56
  cTime: 2022-04-13 11:59:56
  inode: 5629499534431248
  size: 41004
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\xampp\tmp\phpCACD.tmp"
}
Sinnbeck's avatar

@karimali1337 Yeah. But you are checking a for a file input named file not recorder

 public function upload_record(Request $request)
    {

        if($request->hasFile('recorder')){//check if it is in fact a file..

                    $file=$request->file('recorder');
                $path=$request->recorder->storeAs('records',$file->getClientOriginalName());
               //or this way $path=$request->file('recorder')->storeAs('records',$file->getClientOriginalName());
              //or this way $path = $file->->storeAs('records',$file->getClientOriginalName());
                //   $file->move('records', $name);
        }
    }

Please or to participate in this conversation.