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:
Yes. I know it has something to do with the if statement, but really want it to work WITH an if statement.
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:
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.