图像输入不上传

时间:2015-07-08 13:03:00

标签: php file-upload

好吧,我有一个页面可以添加项目,并有一个用于将文件上传到网站的输入。 图片无法上传到网站.. 问题出在哪儿? 代码位于:

之间
##      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 个答案:

答案 0 :(得分:0)

更改您的表单标记

<form action="add.php" method="post">

to

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

并且还要改变

if ($userfile > 0) {

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