Jump to content

basename


davidcriniti

Recommended Posts

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***********************

?>


Link to comment
Share on other sites

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')";

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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! :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.  :P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.