ajicles Posted July 29, 2010 Share Posted July 29, 2010 I need help with my upload script it doesn't want to do anything when button is submitted. <?php if (isset($_POST['submitBtn'])){ require_once("includes/upload.php"); } ?> <div id="submitform"> <h2>Upload:</h2> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br /> Choose a file to upload:<br /> <input name="uploaded_file" type="file" /><br /> <input type="submit" value="Upload" /><br /> </form> upload.php <?php //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='.$file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/ Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2010 Share Posted July 29, 2010 Is this all one script? Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092836 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 Is this all one script? Well. When the uploade botton is pressed it loads the upload script and then executes it and puts in the variables with the data information. If require_once("includes/upload.php"); wasn't inside of a if pressed command it would be like where the information and shoots errors and hopefully the error traps work lol. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092941 Share on other sites More sharing options...
mongoose00318 Posted July 30, 2010 Share Posted July 30, 2010 I need help with my upload script it doesn't want to do anything when button is submitted. <?php if (isset($_POST['submitBtn'])){ require_once("includes/upload.php"); } ?> <div id="submitform"> <h2>Upload:</h2> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br /> Choose a file to upload:<br /> <input name="uploaded_file" type="file" /><br /> <input type="submit" value="Upload" /><br /> </form> upload.php <?php //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='.$file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } ?> Try this : 1) Remove the require_once() 2) Paste your upload file inplace of the require_once() 3) Instead of having if(isset($_POST['submitBtn'])) try this if(isset($_POST)) Let me know if that helps or not Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092944 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 if ($HTTP_POST_VARS['uploaded_file']) { // code of upload.php or require_once("includes/upload.php"); } else { // show form } Check actual submission of form. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092945 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 I've had some time to look this over, and actually, since your submit button has no name attribute, try simply giving it the name="submitBtn" attribute and see if that doesn't fix it. Also, you should get rid of the action="<?php echo $_SERVER['PHP_SELF']; ?>", as it's an XSS risk. Make it simply action="" Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092952 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 Just when I get something working something else screws up lol. So, I made the necessary changes to my script. if(isset($_POST['submitBtn'])) { //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } } else { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action=' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' value='Upload' /><br /> </form>"; } And now when I press the submit button, it redirects me to: http://localhost/FileHosting/method=?MAX_FILE_SIZE=1000000&uploaded_file=text.txt I tried if(isset($_POST)) and if(isset($_POST['submitBtn'])) and if ($HTTP_POST_VARS['uploaded_file']) Only isset works :-\ I though the issue with the redirect error would be if(isset($_POST)) so I changed it back to if(isset($_POST['submitBtn'])) Thanks for your help so far guys Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092966 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 You left out a quote at action=' Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092967 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 I am an idiot sometimes Also dreamweaver tells me: The document's current encoding can not correctly save all the of the characters within the document. You may want to change to UTF-8 or an encoding that supports the special characters in this document. But that's the only error >_>. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092969 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 Fix the last error and it still doesn't process any of the code after being submitted... if(isset($_POST)) { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action='' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' value='Upload' /><br /> </form>"; } else { //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } } Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092971 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 <?php if(isset($_POST)) { //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } } else { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action='' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' value='Upload' /><br /> </form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092973 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 <?php if(isset($_POST)) { //Сheck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "Error: No file uploaded"; } } else { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action='' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' value='Upload' /><br /> </form>"; } ?> WHY does it have to be 497 lines long! Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092975 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 LoL, its because of my code editor software, anyway I figured out what is the problem 1. $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; Check here do you have directories at this location ? Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092979 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 <?php if ($HTTP_POST_VARS['submit']) { if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { } //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action='test.php' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' name='submit' value='Upload' /><br /> </form>"; } ?> I have updated this code. and its working fine here. just replace <form enctype='multipart/form-data' action='test.php' method='post'> test.php here with your script name. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092982 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 Notice: Undefined variable: HTTP_POST_VARS in D:\wamp\www\FileHosting\upload.php on line 37 Line 37 is line 2 in your code you submitted. Thanks for the help so far guy Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092983 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 The problem is that your PHP version and server probably don't support the deprecated $HTTP_POST_VARS collection. as you are running on Windows.. That's probably the source of the issue. Try replacing with $_POST in your code <?php if ($_POST['submit']) { if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { } //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); //$ext = substr($filename, strrpos($filename, '.') + 1); if (($_FILES["uploaded_file"]["size"] < 26214400)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { $ip_address = $_SERVER['REMOTE_ADDR']; $file_hash = hash('ripemd160', '$filename'); echo 'Link to your file: <a href="http://localhost/filehosting/download.php?d='. $file_hash.'">http://localhost/filehosting/download.php?d='.$file_hash.'</a>'; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("filehosting"); $sql="INSERT INTO data(ID, file_location, file_hash, file_ip_address) VALUES ('','$filename','$file_hash','$ip_address')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only files under 25MBs are accepted for upload"; echo 'Upgrade your account to upload bigger files.'; } } else { echo "<h2>Upload:</h2> <form enctype='multipart/form-data' action='upload.php' method='post'> <input type='hidden' name='MAX_FILE_SIZE' value='1000000' /><br /> Choose a file to upload:<br /> <input name='uploaded_file' type='file' /><br /> <input type='submit' name='submit' value='Upload' /><br /> </form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092984 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 LoL, its because of my code editor software, anyway I figured out what is the problem 1. $newname = dirname(__FILE__).'/upload/'.$_SESSION['username'].''.$filename; Check here do you have directories at this location ? Yes. Also there is suppose to be a / separating the username and the filename lol. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092985 Share on other sites More sharing options...
ajicles Posted July 30, 2010 Author Share Posted July 30, 2010 Thank you for all your help guys. My script is working greatly now. I had to change if ($_POST['submit']) to if (isset($_POST['submit'])). But thank you so much for your help Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1092987 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 $HTTP_POST_VARS was deprecated as of PHP 4.1.0, if I recall correctly. use $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/209284-file-upload/#findComment-1093113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.