dtpd58676 2015-07-08 13:03
浏览 29

图像输入不上传

Well, I have page to add items, and have an input for uploading a file to the site. The Image doesn't upload to the site.. Where is the problem? The code is between the:

##      IMAGE       IMAGE       IMAGE       IMAGE       IMAGE       ##

add.php

 <form action="add.php" method="post">
  <input name="title_name" type="text" class="form-control" style="width: 250px;margin-left:auto;margin-right:auto;display:inline;" placeholder="שם הפריט או נושא" /> <br />
  <textarea name="description" class="form-control" rows="5" placeholder="תיאור הפריט..." style="width: 250px;margin-left:auto;margin-right:auto;display:inline;"></textarea> <br />
  <input class="btn btn-primary" name="uploadedfile" type="file" style="width: 250px;margin-left:auto;margin-right:auto;display:inline;">   <br />
  <input name="type" type="text" class="form-control" style="width: 250px;margin-left:auto;margin-right:auto;display:inline;" placeholder="סוג הפריט" /> <br />
  <button type="submit" class="btn btn-primary" name="submitAdd">הוסף פריט</button>
  </form>
  <br>
  <div class="container">

  <?php 
  $title_name = $_POST['title_name'];
  $description = nl2br($_POST['description']);
  $username = $_SERVER['REMOTE_ADDR'];
  $type = $_POST['type'];
  $ok = 1;
  if(isset($_POST['submitAdd'])) {
  if(empty($title_name)) {
  echo '<div class="alert alert-danger fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>שגיאה:</strong> The name of item can stay empty
  </div>';
  $ok = 0;
  }
  if(!empty($title_name) && !preg_match("/[A-Za-z0-9א-ת\.\,\_\- ]/", $title_name)) {
  echo '<div class="alert alert-danger fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>שגיאה:</strong> שם הפריט מכיל תווים לא מורשים
  </div>';
  $ok = 0;
  }
  if(!empty($description) && !preg_match("/[A-Za-z0-9א-ת\.\,\_\- ]/", $description)) {
  echo '<div class="alert alert-danger fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>שגיאה:</strong> תיאור הפריט מכיל תווים לא מורשים
  </div>';
  $ok = 0;
  }

if (!empty($userfile) && !preg_match('/^(?:[a-z0-9_-]|\.(?!\.))+$/iD', $userfile)) {
    echo '<div class="alert alert-danger fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>שגיאה:</strong> Error with name of file
  </div>';
  $ok = 0;
}
    ##      IMAGE       IMAGE       IMAGE       IMAGE       IMAGE       ##
//
    $uploadImageStatus = 1;
    $userfile = $_POST['uploadedfile'];
    $name = strtolower($_FILES['uploadedfile']['name']);
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $allow = array("png", "jpeg", "jpg");
    $target_path = 0;

    if ($userfile > 0) {
    if(!in_array($ext, $allow)) {
        echo '<div class="alert alert-danger fade in">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        <strong>שגיאה:</strong> This file type dont allowed
        </div>';
        $uploadImageStatus = 0;
        }
        if($uploadImageStatus !== 0 && $_FILES['uploadedfile']['error'] !== 0) {
        $nameFile = $_FILES['uploadedfile']['name'];
        $target_path = "images/".basename(md5($_FILES['uploadedfile']['name']).time()).".$ext";
            if(move_uploaded_file($nameFile, $target_path)) { 
                $uploadImageStatus2 = 1; 
            }
        else {
            $uploadImageStatus2 = 0;
            echo '<div class="alert alert-danger fade in">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            <strong>שגיאה:</strong> Error on try upload this image
            </div>';
        }
    }
}
##      IMAGE       IMAGE       IMAGE       IMAGE       IMAGE       ##
  • 写回答

1条回答 默认 最新

  • dongwu3596 2015-07-08 13:17
    关注

    change you form tag

    <form action="add.php" method="post">
    
    to
    
    <form action="add.php" method="post" enctype="multipart/form-data">
    

    and also change

    if ($userfile > 0) {
    

    to

    if (!empty($_FILES["uploadedfile"])) {
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路