php文件上传

Php code

$target = "upload/";
$nameF = "";

$targetImage = "upload/";
$nameI = "";

if (!empty($_FILES['fileUP']['name'])) {
  print_r("ce il file");
  $target = $target . basename($_FILES['fileUP']['name']);
  $nameF = $_FILES['fileUP']['name'];
  if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
    echo -1;
  }
}

if (!empty($_FILES['imageUP']['name'])) {
  $targetImage = $targetImage . basename($_FILES['imageUP']['name']);
  $nameI = $_FILES['imageUP']['name'];
  if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
    echo -1;
  }
}

$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];


$sql = "INSERT INTO  news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());

if ($result === TRUE) {

  echo 1;
} else {
  echo -1;
}

Form

<form enctype="multipart/form-data" id="insert" class="bs-example bs-example-form" method="POST">
    <div class="input-group">
        <span class="input-group-addon">Titolo</span>
        <input id="title" name="title" type="text" class="form-control" placeholder="Titolo">
    </div>

    <br>

    <div class="input-group">
        <span class="input-group-addon">Admin</span>
        <input id="admin" name="admin" type="text" class="form-control" value='{{$utente|lower}}'
               placeholder='{{$utente}}'>
    </div>

    <br><br> <br> <br>

    <div class="input-group">

        <span class="input-group-addon">Immagine</span>
        <input id="image" name="imageUP" accept="image/*" type="file" class="form-control"
               placeholder="Immagine">

    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon">File</span>
        <input id="image" name="fileUP" id="fileToUpload" type="file" class="form-control"
               placeholder="FIle">
    </div>

    <br>
    <div class="input-group">
        <span class="input-group-addon"><span class="glyphicon glyphicon-font"></span></span>
        <input id="content" name="content" type="text" class="form-control"
               placeholder="Contenuto">
    </div>
    <br>
    <button id="crea" type="submit" class="btn btn-warning">Crea</button>
</form>

Ajax request

$('#insert').submit(function (e) {
  e.preventDefault();
  var data = new FormData($(this)[0]);

  $.ajax
  ({
    url: 'uploads.php',
     data: data,
     type: 'post',
     processData: false,
     contentType: false,
     success: function (response) {
    response = parseInt(response);
    switch (response) {
    case -1: //errore generico
    alert("errore");
    break;
    case 1:
    alert("la creazione della news è andata a buon fine");
    break;
    }

  close ajax call..

My problem is: the script work but not well, i've notice that query don't insert data if i put text in the 'content' input and upload image.

In the console I've this error:

not allowed to load local resource: file:///C:/fakepath/xx.jpg

when i work in localhost i haven't this error and query ALWAYS insert data. Now i've problem and i am in real server.

Anyone know to fix it?ù I need your help

Try this. And, let me know. Use whole code as it is. I will explain in few minutes. First try.

$target = "upload/";
$nameF = "";

$targetImage = "upload/";
$nameI = "";

$flag = 1;

if (!empty( $_FILES['fileUP']['name'])) {
    print_r("ce il file");
    $target = $target . basename( $_FILES['fileUP']['name']);
    $nameF =$_FILES['fileUP']['name'];
    if (!move_uploaded_file($_FILES['fileUP']['tmp_name'], $target)) {
      $flag = -1;
    }
}

if (!empty( $_FILES['imageUP']['name'])) {
    $targetImage = $targetImage . basename( $_FILES['imageUP']['name']);
    $nameI =$_FILES['imageUP']['name'];
    if (!move_uploaded_file($_FILES['imageUP']['tmp_name'], $targetImage)) {
      $flag = -1;
    }
}

$title = $_POST['title'];
$admin = $_POST['admin'];
$content = $_POST['content'];

$sql = "INSERT INTO  news (title,admin,content,img,file) values('$title','$admin','$content','$nameI','$nameF')";
$result = $conn->query($sql) or die(mysql_error());

if ($result === TRUE) {
  $flag = 1;
}
else {
  $flag = -1;
}

if($flag == -1){
  echo -1;
} else {
  echo 1;
}