简体   繁体   中英

What are the reasons of failing move_uploaded_file() in PHP when the website is hosted online?

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.

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"];
}

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM