1

I have written a code for uploading an Image using PHP, it works fine on my localhost server but the function move_uploaded_file() is returning false when I have uploaded the website on an Online Server.

Here is my HTML code

<input type="file" name="selectImg" id="selectImg" accept="image/jpg, image/jpeg" style="display: none;">
<label for="selectImg" class="btn btn-primary" style="border-radius: 29px; outline: none;" > Chose a Image</label>
<button class="btn btn-success" style="border-radius: 29px;outline: none;" id="upload">Upload</button>

AJAX function for sending the data

function sendData(name,pid,file){
  let data = new FormData();
  data.append('file',file);
  data.append('pid',pid);
  data.append('name',name);

  console.log(data);
  $.ajax({
    type: "post",
    url: "upload.php",  
    data :data,
    processData: false,
    contentType: false,
    success: function(data) {
      // if(data=='UPLOADED SUCCESSFULLY'){
      //   alert(data);
      //   location.reload();
      // }
      alert(data);
    }
  });
}

Here is my JavaScript Code

uploadBtn.addEventListener("click",function(){

  if(file){
    
    let fName = file['name'].split(".");
    // console.log(fName);

    if(fName[1] == "jpeg" || fName[1] == "jpg"){

      let pid = document.getElementById('pID').value;

      let name = file['name'];
      let url = window.location.href.split("=");
      
      sendData(name,pid,file);
      
      
    }else{
      alert('Please Select a jpeg/jpg file');
    }


  }else{
    alert('Please Select an image');
  }

});

Here is the PHP CODE

if(isset($_POST['pid'])){

    $pid = $_POST['pid'];
    $imgFile = $_FILES['file'];
    $iName = $_POST['name'];
    $loc = $_FILES['file']['tmp_name'];
    $path = "../assets/uploads/".basename($_FILES['file']['name']);
    $serverPath = "assets/uploads/".$iName;
    $size = filesize($loc);
    if(move_uploaded_file($loc,$path)){
        $pathquery = "INSERT INTO `images` (`path`, `pid`, `status`) VALUES ('$serverPath', '$pid', 'ACTIVE');";
        echo $pathquery;
        $sendPathQuery = mysqli_query($conn,$pathquery);
    }else{
        echo "File Not Uploaded";
    }
}else{
    echo "FILE NOT UPLOADED !!!";
}

The above code works perfectly on the localhost server but when uploaded the move_uploaded_file() function is returning false.

  • You need to check out who is the user that is running the php (use phpinfo() to understand that) and you need to understand what are the permissions of the uploads folder and see if you can give the php user write access to this folder. – Gal Gur-Arie Jul 26 '21 at 16:03
  • Can you please tell me how to achiive that. – Soumya Darshan Jul 26 '21 at 16:08

3 Answers3

1

Check that the web server has permissions to write to the /assets/uploads/ directory

Or add this to get a comprehensive answer :

$moved = move_uploaded_file($loc,$path);

if( $moved ) {
  echo "Successfully uploaded";         
} else {
  echo "Not uploaded because of error #".$_FILES["file"]["error"];
}
Reynadan
  • 649
  • 4
  • 18
0

move_uploaded_file :

If from is not a valid upload file, then no action will occur, and move_uploaded_file() will return false.

If from is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return false.

Check permissions on the file and directories. See:

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
0

I have changed the file permission using FTP and after that it worked with me.