Upload Problem!?

Discussion in 'PHP' started by ddtader, May 19, 2010.

  1. #1
    I can't seem to find the problem with this code, although it is not uploading correctly.

    <?php
    function upload ( ) {
    	$upload = "<form enctype='multipart/form-data' action='/' method='post'><input name='ufile' type='file'><br><br><input type='submit' name='upload' value='Upload'></form>";
    	if ( isset ( $_POST['upload'] ) ) {
    		if ( is_uploaded_file ( $_FILES['ufile']['tmp_name'] ) ) {
    			move_uploaded_file ( $_FILES['ufile']['tmp_name'], "/var/www/upload".'/'.$_FILES['ufile']['name'] );
    			if ( file_exists ( "/var/www/upload/".$_FILES['ufile']['name'] ) ) {
    				echo '<br>Upload successful!<br><br><a href="upload/'.$_FILES['ufile']['name'].'">localhost/upload/'.$_FILES['ufile']['name'].'</a>';
    			} else {
    				echo "Error!<br>";
    			}
    		} else {
    			echo "Error!<br>";
    		}
    	} else {
    		echo $upload;
    	}
    }
    $form = "<form method='post'><input type='submit' name='submit' value='submit'></form>";
    if ( isset ( $_POST['submit'] ) ) {
    	upload ( );
    } else {
    	echo $form;
    }
    ?>
    
    PHP:
     
    ddtader, May 19, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    there are many possibilities on why this script isnt working.. did you try this in your local???
     
    bartolay13, May 19, 2010 IP
  3. ddtader

    ddtader Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes. I know it has something to do with the if statement, but really want it to work WITH an if statement.
     
    ddtader, May 19, 2010 IP
  4. friendishan

    friendishan Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It really seems to be a problem with the if statement
     
    friendishan, May 19, 2010 IP
  5. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #5
    do it like this:
    
    <?php
    function upload ( ) {
        $upload = "<form enctype='multipart/form-data' action='' method='post'><input name='ufile' type='file'><br><br><input type='hidden' name='submit' value='1' /><input type='submit' name='upload' value='Upload'></form>";
        if ( isset ( $_POST['upload'] ) ) {
            if ( is_uploaded_file ( $_FILES['ufile']['tmp_name'] ) ) {
    			$dir = dirname(__FILE__) . '/upload/';
                move_uploaded_file ( $_FILES['ufile']['tmp_name'], $dir.$_FILES['ufile']['name'] );
                if ( file_exists ( $dir.$_FILES['ufile']['name'] ) ) {
                    echo '<br>Upload successful!<br><br><a href="upload/'.$_FILES['ufile']['name'].'">localhost/upload/'.$_FILES['ufile']['name'].'</a>';
                } else {
                    echo "Error!<br>";
                }
            } else {
                echo "Error!<br>";
            }
        } else {
            echo $upload;
        }
    }
    $form = "<form method='post'><input type='submit' name='submit' value='submit'></form>";
    if ( isset ( $_POST['submit'] ) ) {
        upload ( );
    } else {
        echo $form;
    }
    ?>
    
    PHP:
     
    gapz101, May 19, 2010 IP
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Yeah gapz code will work mostly for this problem! :)
     
    roopajyothi, May 19, 2010 IP
  7. ddtader

    ddtader Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks gapz101, that solves the above code problem, but doesn't quite help my situation. I guess I should have posted more example code to reproduce what was occuring.

    How would I get the above code to work, but with this if statement instead?:

    
    $form = "<form method='post'><input type='text' name='input'><input type='submit' name='submit' value='submit'></form>";
    if ( isset ( $_POST['submit'] ) ) {
    	if ( $_POST['input'] == "upload" ) {
    		upload ( );
    	} else {
    		echo $form;
    	}
    } else {
    	echo $form;
    }
    PHP:
    It's not my exact code, but like I said, should be able to reproduce the problem.

    Thanks in advanced! Any help is appreciated.
     
    ddtader, May 20, 2010 IP
  8. ddtader

    ddtader Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Nevermind, I found a solution.
     
    Last edited: May 24, 2010
    ddtader, May 24, 2010 IP