weixin_33725239 2016-09-10 12:56 采纳率: 0%
浏览 15

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

  • 写回答

1条回答 默认 最新

  • ?Briella 2016-09-10 13:13
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度