davidcriniti Posted January 3, 2012 Share Posted January 3, 2012 Hi, I have a form which asks people to upload a php file. I'd like the file to be uploaded to a directory and the filename, minus the extension, to be inserted into the 'pagenameselect' field in a table in my database. The page is uploading to the directory fine, but the basename is not being inserted into the database. Any tips on where I'm going wrong with my code? <?php global $_POST; $upload_page = $_POST["upload_page"]; $pagenameselect = basename($upload_page, '.php'); $uploadED_page=$_FILES['upload_page']['name']; if($uploadED_page!="") { if (!copy($_FILES['upload_page']['tmp_name'], "$uploadED_page")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO editablepageslist (pagenameselect)" . "VALUES ('$pagenameselect')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/ Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 Global $_POST = no no. $_POST is already a global array. This line, $query = "INSERT INTO editablepageslist (pagenameselect)" . "VALUES ('$pagenameselect')"; Shoul be,& $query = "insert into editablepagelist (pagenameselect) values ('$pagenameselect')"; Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303522 Share on other sites More sharing options...
davidcriniti Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks AyKay47, but it is still not working. I was thinking the error may have been in this line. <?php $pagenameselect = basename($upload_page, '.php'); ?> Does that look ok to you? Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303529 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 @AyKay - Both of you $query variables will output pretty much the same thing as well as do the same thing when queried with MySQL, that has no bearing on the issue. Personally I use the original style, but I like to add back tics as well as parenthesis around variables. $query = "INSERT INTO `editablepageslist` (`pagenameselect`) VALUES ('{$pagenameselect}')"; I have a feeling it's do to with the with form data being sent, could you possibly show us the form that is sending the data? Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303530 Share on other sites More sharing options...
davidcriniti Posted January 3, 2012 Author Share Posted January 3, 2012 Hi Paul, Here's the form: <form action="upload_page_confirm.php" method="post" enctype="multipart/form-data"> <p> </p> <p>Upload page: <span id="sprytextfield2"> <label for="upload_page"></label> <input type="file" name="upload_page" id="upload_page" /> <span class="textfieldRequiredMsg">Please select a page to upload.</span></span></p> <p> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> <input type="reset" name="reset" id="reset" value="Reset" /> </p> </form> <p> </p> <script type="text/javascript"> var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2"); </script> The file uploads ok, but the name doesn't go into the database. That's why I suspected it might have been the basename line in the php code. Also because I've only come across basename via Google today, so am not really sure what I'm doing with it! :-) Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303534 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 As I suspected, you try to get "$_POST['upload_page']" but that does not exist, you should be calling "$_FILES['upload_page']['name']". Use the "$_FILES['upload_page']['name']" as the "$upload_page" like this: $upload_page = $_FILES['upload_page']['name']; echo $upload_page; Then once you know the value of $upload_page, you can then manipulate it to get the value you require. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303536 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 @AyKay - Both of you $query variables will output pretty much the same thing as well as do the same thing when queried with MySQL, that has no bearing on the issue. Personally I use the original style, but I like to add back tics as well as parenthesis around variables. $query = "INSERT INTO `editablepageslist` (`pagenameselect`) VALUES ('{$pagenameselect}')"; I have a feeling it's do to with the with form data being sent, could you possibly show us the form that is sending the data? Regards, PaulRyan. Is the same yes, however it's a bad practice to concatenation for no reason, the query you posted does the exact same thing as well. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303541 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 Indeed it does, I was just showing how I prefer to write my statements, nothing meant by it Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303542 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 Indeed it does, I was just showing how I prefer to write my statements, nothing meant by it That is the same thing I was doing, I found it funny that you called me out on something that you went ahead and did in the same post. It's cool, was just stating. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303544 Share on other sites More sharing options...
davidcriniti Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks to both of you. Here is the latest code, and it now works perfectly: <?php $_POST; $upload_page = $_FILES['upload_page']['name']; $pagenameselect = basename($upload_page, '.php'); $uploadED_page=$_FILES['upload_page']['name']; if($uploadED_page!="") { if (!copy($_FILES['upload_page']['tmp_name'], "$uploadED_page")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "insert into editablepageslist (pagenameselect) values ('$pagenameselect')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Thanks again, Dave Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303547 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 No problem, glad to have helped Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303548 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 Thanks to both of you. Here is the latest code, and it now works perfectly: <?php $_POST; $upload_page = $_FILES['upload_page']['name']; $pagenameselect = basename($upload_page, '.php'); $uploadED_page=$_FILES['upload_page']['name']; if($uploadED_page!="") { if (!copy($_FILES['upload_page']['tmp_name'], "$uploadED_page")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "insert into editablepageslist (pagenameselect) values ('$pagenameselect')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Thanks again, Dave $_POST; ? Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303554 Share on other sites More sharing options...
davidcriniti Posted January 3, 2012 Author Share Posted January 3, 2012 I changed it from Global $_POST to $_POST as you said Global $_POST = no no Did is mis-interpret? Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303556 Share on other sites More sharing options...
KevinM1 Posted January 3, 2012 Share Posted January 3, 2012 I changed it from Global $_POST to $_POST as you said Global $_POST = no no Did is mis-interpret? Yes you did. $_POST is a superglobal array. As such, it is always directly accessible, meaning you don't have to declare it before using it. Beyond that, NEVER use the 'global' keyword. It's a bad/sloppy way of passing variables around. If you're learning PHP from a resource that uses 'global', get a better resource. 'global' is an indicator of an amateur. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303558 Share on other sites More sharing options...
davidcriniti Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks for the tip, so latest code is: <?php $upload_page = $_FILES['upload_page']['name']; $pagenameselect = basename($upload_page, '.php'); $uploadED_page=$_FILES['upload_page']['name']; if($uploadED_page!="") { if (!copy($_FILES['upload_page']['tmp_name'], "$uploadED_page")) { echo "failed to copy \n"; } } //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "insert into editablepageslist (pagenameselect) values ('$pagenameselect')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); //***********************END OF DATABASE CODE*********************** ?> Hopefully that's now free of any amateur mistakes! Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303561 Share on other sites More sharing options...
AyKay47 Posted January 3, 2012 Share Posted January 3, 2012 I changed it from Global $_POST to $_POST as you said Global $_POST = no no Did is mis-interpret? Yes you did. $_POST is a superglobal array. As such, it is always directly accessible, meaning you don't have to declare it before using it. Beyond that, NEVER use the 'global' keyword. It's a bad/sloppy way of passing variables around. If you're learning PHP from a resource that uses 'global', get a better resource. 'global' is an indicator of an amateur. For clarification on the consequences of using the global keyword, refer to KevinM1's signature. Quote Link to comment https://forums.phpfreaks.com/topic/254240-basename/#findComment-1303562 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.