文件上传失败php

时间:2016-11-01 07:24:30

标签: php

它一直在说失败,我失去了理智我尝试了每一个修复和Google搜索我可以想到表单中的名称是func randomStringWithLength(len: Int) -> NSString { let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" let randomString : NSMutableString = NSMutableString(capacity: len) for _ in 0 ..< len { let length = UInt32 (letters.length) let rand = arc4random_uniform(length) randomString.appendFormat("%C", letters.characterAtIndex(Int(rand))) } return randomString }

这是我的脚本很抱歉,我在手机上贴了这个不好的缩进

file

以下是相关的html

        <?
         if(isset ($_POST ['submit'])){
         $name = $_POST ['name'];
         $pub = $_POST ['author'];
         $price = $_POST ['price'];
         $size = $_POST ['size'];
         $icon = $_POST ['icon'];

           $fname = str_replace (" ", "", $name);
           $make = mkdir  ("/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname");
           $dir = "/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname";
           $source = "/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname/$fname.apk";
          $file = $_FILES ["file"]["tmp_name"];
          $filen = $_FILES ["file"]["name"];
                   if(move_uploaded_file($file,"/membri/deathcrow/0android/appmarket/appmarket/downloads/ ". $fname . $filen)){
        /*$sql = "INSERT INTO appmarketapps SET name='$name', publisher='$pub', price='$price', picture='$icon', src='$source'";
             $query = mysql_query ($sql); */
             echo "<script>alert ('Success'); </script>";
              }else {
        echo "<script>alert ('Failure'); </script>";
              }
            }
            ?>

4 个答案:

答案 0 :(得分:1)

上传文件时需要将enctype设置为multipart/form-data,如下所示:

   <!-- form start -->
    <form method='post' role="form" enctype="multipart/form-data"> 
      <div class="box-body">
      <div class="form-group">
          <label for="appname">Name:</label>
          <input type="text" name='name' class="form-control" id="appname" placeholder="Enter App Name">
        </div>
        <div class="form-group">
          <label for="publisher">Publisher:</label>
          <input type="text" name='author' class="form-control" id="publisher" placeholder="App Publisher">
        </div>
        <div class="form-group">
          <label for="publisher">Price:</label>
          <input type="text" name='price' class="form-control" id="publisher" placeholder="App Price">
        </div>
        <div class="form-group">
          <label for="size">Size:</label>
          <input type="text" name='size' class="form-control" id="size" placeholder="Size (KB, MB, GB)">
        </div>
        <div class="form-group">
          <label for="icon">Icon:</label>
          <textarea name='icon' rows="5" cols="80" class="form-control" id="icon" placeholder="Icon..."></textarea>
        </div>
        <!-- select -->
        <div class="form-group">
          <label>Select Category:</label>
          <select class="form-control" name='catagory'>
            <option>Games</option>
            <option>Utilities</option>
            <option>Applications</option>
            <option>System</option>
            <option>Other/Uncategorized</option>
          </select>
        </div>
        <div class="form-group">
          <label for="fileupload">File upload</label>
          <input type="file" name='file' id="fileupload">

          <p class="help-block">Upload Application</p>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox"> Is this an update?
        <p class="help-block">Only check this box if the file being uploaded is an update. (i.e. v0.2.1 --> v0.2.2)</p>
          </label>
        </div>
      </div>
      <!-- /.box-body -->

      <div class="box-footer">
        <button type="submit" name='submit' class="btn btn-primary">Submit</button>
        <button type="reset" class="btn btn-danger">Reset</button>
      </div>
    </form>
  </div>
  <!-- /.box -->



<?php
    if( isset( $_POST['submit'], $_POST['name'], $_POST['author'], $_POST['price'], $_POST['size'], $_POST['icon'], $_FILES['file'] ) ){

         $name      = $_POST ['name'];
         $pub       = $_POST ['author'];
         $price     = $_POST ['price'];
         $size      = $_POST ['size'];
         $icon      = $_POST ['icon'];

         $file      = (object)$_FILES['file'];
         $ftmp      = $file->tmp_name;
         $fname     = $file->name;
         $fsize     = $file->size;
         $ferror    = $file->error;
         $ftype     = $file->type;



         if( is_uploaded_file( $ftmp ) && $ferror == UPLOAD_ERR_OK ){

            $basepath   = '/membri/deathcrow/0android/appmarket/appmarket/downloads/';
            $filename   = str_replace( " ", "", $name );
            $dir        = $basepath . $filename;

            /* If the folder does not already exist, create it */
            if( !file_exists( $dir ) ) mkdir( $dir );
            clearstatcache();


            $source     = $dir . DIRECTORY_SEPARATOR . "$filename.apk";
            $result     = @move_uploaded_file( $tmp, $source );

            if( $result ){
                $sql = "INSERT INTO `appmarketapps` SET `name`='$name', `publisher`='$pub', `price`='$price', `picture`='$icon', `src`='$source';";
                echo $sql;
            } else {
                echo 'error: unable to move file';
            }

            exit( "<script>alert('".( $result ? 'success' : 'failure' )."')</script>" );
         } else {
            echo 'error: possible file upload attack or other error';
         }
    }
?>

答案 1 :(得分:0)

if(move_uploaded_file($file,"/membri/deathcrow/0android/appmarket/appmarket/downloads/ ". $filen))

而不是

if(move_uploaded_file($file,"/membri/deathcrow/0android/appmarket/appmarket/downloads/ ". $fname . $filen))

答案 2 :(得分:0)

检查您是否具有该目录的写入权限,并将权限777用于测试目的,但将其更改为您需要的工作。还请发布html给我们一个清晰的图片。

答案 3 :(得分:0)

  
    

首先检查文件权限。

         

对于要存储上传文件的目录源路径,请使用绝对路径。您可以使用getcwd()函数来获取绝对路径。

         

从使用move_uploaded_file的if语句中删除空格     像

  
<?
    if(isset ($_POST ['submit'])){
        $name = $_POST ['name'];
        $pub = $_POST ['author'];
        $price = $_POST ['price'];
        $size = $_POST ['size'];
        $icon = $_POST ['icon'];

        $fname = str_replace (" ", "", $name);
        $make = mkdir("/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname");
        $dir = "/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname/";
        $source = "/membri/deathcrow/0android/appmarket/appmarket/downloads/$fname/$fname.apk";
        $file = $_FILES ["file"]["tmp_name"];
        $filen = $_FILES ["file"]["name"];
        if(move_uploaded_file($file,"/membri/deathcrow/0android/appmarket/appmarket/downloads/". $fname . $filen)){
            /*$sql = "INSERT INTO appmarketapps SET name='$name', publisher='$pub', price='$price', picture='$icon', src='$source'";
            $query = mysql_query ($sql); */
            echo "<script>alert ('Success'); </script>";
        }else {
            echo "<script>alert ('Failure'); </script>";
        }
    }
?>

还添加表单属性enctype= multipart/form-data以上传文件