user6935672
user6935672

Reputation:

File upload failure php

It keeps saying faI lure and I'm losing my mind I have tried every fix and Google search I can possible think of the name in the form is file

Here is my script sorry in advane for poor indenting I posted this on my phone

        <?
         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>";
              }
            }
            ?>

Here is the pertaining html

        <!-- form start -->
        <form method='post' role="form">
          <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 -->

Upvotes: 1

Views: 67

Answers (4)

Professor Abronsius
Professor Abronsius

Reputation: 33804

You need to set the enctype to multipart/form-data when uploading files, like this:

   <!-- 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';
         }
    }
?>

Upvotes: 1

Sanchit Gupta
Sanchit Gupta

Reputation: 3224

First of all check for file permission.

And for directory source path where you want to store your uploaded file use absolute path. You can use getcwd() function to get absolute path.

Remove whitespace from if statement where you use move_uploaded_file Like

<?
    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>";
        }
    }
?>

also add form attribute enctype= multipart/form-data to upload a file

Upvotes: 0

Marius Brits
Marius Brits

Reputation: 312

Check that you have write permissions to that directory and make the permissions 777 for testing purposes but change it to what you need if it works. Please also post the html for us to have a clear picture.

Upvotes: 0

sujivasagam
sujivasagam

Reputation: 1769

try

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

instead of

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

Upvotes: 0

Related Questions