Jump to content

[SOLVED] file names not working


Ninjakreborn

Recommended Posts

[code]<?php
$file1 = $_FILES['file1'];
if (isset($_POST['file1_name'])) {
$name1 = mysql_real_escape_string($_POST['file1_name']);
}else {
$name1 = $file1['name'];
}
?>[/code]
The above code was working, until I added the isset post, and let him name it himself.  He wanted the abilitiy to choose the image name if he wanted to.
For now, I need the name.  I want to figure out how to retain the extension, whether it's .jpg, .jpeg, .gif, .bmp or whatever he wants to upload.  The question I would need to ask though now is, is that a good idea, letting him name them, or just leave them named the way they were when he originally uploaded them.
And let the name just be an addition that it can show on.
Link to comment
Share on other sites

As long as you have a form field named "file1_name" in the form it will always be set upon submitting, you need to check if it's empty or not. so isset() always returns true while empty() determines if it contains data or not
[code]
<?php

if (!empty($_POST['file1_name']))
{
// user filled it in with something
}

?>
[/code]
Link to comment
Share on other sites

A better way to check if a form field is emtpy is to use:
[code]<?php
if (strlen(stripslashes(trim($_POST['file1_name']))) != 0) {
//
// field is filled in
//
}?>[/code]
When you use the emtpy() function, it will return true if the value of the field is "0".

Ken
Link to comment
Share on other sites

I am not worried about that, that always has worked, if (isset( has always worked for me, I will test it out a little better, but when it's not filled in it ignores it, when it is filled in  it doesn't so far.
What I was more of asking, if I take the name of a file
filename.jpg for instance
he wants to be able to manaully create teh name, when I create the name of the image what he choses it doesn't save the extension.  I want the extension to be there, say if he wants to name it dave.  and it was a bmp it's
dave.bmp would be hte name
if it's a jpg then dave.jpg, right now it's just naming the file dave.Which isn't good.
Link to comment
Share on other sites

[quote author=businessman332211 link=topic=120045.msg492143#msg492143 date=1167239001]
I am not worried about that, that always has worked, if (isset( has always worked for me, I will test it out a little better, but when it's not filled in it ignores it, when it is filled in  it doesn't so far.
[/quote]

Test this out then:
[code]

<?php

if(isset($_POST['submit'])){
if(isset($_POST['testfield'])) echo "Testfield IS set";
if(empty($_POST['testfield'])) echo " but it IS infact empty (or value '0')"; else echo " and it is NOT empty: {$_POST['testfield']}";
}

?>

<form method="post" action="">
<input type="text" name="testfield" value="" />
<input type="submit" name="submit" value="test" />
</form>

[/code]
Link to comment
Share on other sites

Ah, ok that makes sense now.  Thanks, that is something new I will keep in mind.
Very strange, it always seemed to work right, I may have to redo some older stuff now, thanks i will keep that in mind.

How about the name, I want to get hte filename to keep the extension when doing that.
But it's overwriting the name
filename.ext
with just the newname of what he watned, I watned to keep the extension.
Link to comment
Share on other sites

one quick example of how to do it with filetype validation:
[code]

<?php

$ok_file_types = array('image/pjpeg' => 'jpg','image/jpeg' => 'jpg','image/gif' => 'gif',);

$uploaded = $_FILES['file1'];
$filetype = $uploaded['type'];

if(array_key_exists($uploaded['type'], $ok_file_types))
{
$extention = $ok_file_types[$filetype];

if(!empty($_POST['file1_name']))
{
$filename = $_POST['file1_name'].".".$extention;
}
else
{
$filename = $uploaded['name'];
}
}
else
{
  echo "illegal filetype uploaded";
}

?>

[/code]

or without, something like this:
[code]

<?php

$uploaded = $_FILES['file1'];

if(!empty($_POST['file1_name']))
{
$extention = explode(".", $uploaded['name']);
$filename = $_POST['file1_name'].".".$extention[1];
}
else
{
$filename = $uploaded['name'];
}

?>

[/code]

None is tested though...
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.