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

behnampmdg3's avatar

Simple file upload with php

Hi;

$(document).ready(function (e) {
    $('#imageUploadForm').on('submit',(function(e) {
        e.preventDefault();
        var formData = new FormData(this);

        $.ajax({
            type:'POST',
            url: $(this).attr('action'),
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(data){
                console.log("success");
                console.log(data);
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });
    }));

    $("#ImageBrowse").on("change", function() {
        $("#imageUploadForm").submit();
    });
});
move_uploaded_file($_FILES['file']['tmp_name'], '/uploads');

I get

move_uploaded_file(/uploads): failed to open stream: Permission denied

I understand this is a permissions issue. How can I change the permissions with PHP, perhaps before move_uploaded_file? Chmod didn't work.

Thanks

0 likes
5 replies
jlrdw's avatar

@Cronix I think OP is trying to use ajax. So far the two links I shared seem about the two better ones on the forum so far.

Please, if you have a better example or link please share here, as I save many of the good answers to help me and others when possible.

Cronix's avatar
Cronix
Best Answer
Level 67

@jlrdw of course he's using ajax, but that's not the problem so I don't think your links about ajax will help

move_uploaded_file(/uploads): failed to open stream: Permission denied

is the problem, which is what I was addressing. It's on the php end receiving the file, not the ajax end sending it, and it's most likely due to what I said, /uploads is not the right place as it's off the root of the webserver, not his laravel install.

Snapey's avatar

You cannot change the permissions from within the script. If you could, it would make permissions pointless?

Just make sure the place you are saving the file is correct. Appreciate the difference between a URL and a file system path.

Please or to participate in this conversation.