2

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

9
  • I just tried it and it worked , I tried again and it did not work. IT's so strange! Commented Sep 10, 2016 at 12:59
  • when the script didn't work, in the console i see this request has no preview available, and no -1 Commented Sep 10, 2016 at 13:01
  • Is upload folder having read & write permissions? Commented Sep 10, 2016 at 13:02
  • yes , i can upload image with no data(no text in input) Commented Sep 10, 2016 at 13:05
  • if i have text in input the script work 2 times out of 7 Commented Sep 10, 2016 at 13:06

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.