Jump to content


Photo

File Related


  • Please log in to reply
24 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 05:00 PM

I have a question, I don't want to be flat out told this, because I am working on this, trying to learn, I just want some quick guidance, pointed towards a function, or group of functions(preferably in the core php language), if not I can check on extensions. I am wanting to find a way, for me to take an array, and use that array to search a string for the existence of the array characters, for instance.
I have a list of extensions I Want to accept as file attachments, I just need some kind of function I can use to create a construct to run all of those extensions through the string $_FILES['file'] I already checked to see if it was uploaded first is_file_uploaded
function so now I am working on a portion of my script to screen out anything but those filetypes.
THanks. If you can point me to something, doesn't matter which area, I am already familiar with regex, and everything.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 19 June 2006 - 05:05 PM

Probably want to look into [a href=\"http://uk2.php.net/manual/en/function.in-array.php\" target=\"_blank\"]in_array[/a]

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 05:08 PM

If you have the acceptable file extensions in the array, you should use the function [a href=\"http://www.php.net/pathinfo\" target=\"_blank\"]pathinfo()[/a] to get the extension of the uploaded file and the function [a href=\"http://www.php.net/in_array\" target=\"_blank\"]in_array()[/a] to see if that extension is in your array.

Ken

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 05:11 PM

Thanks exactly what I was looking for, I have some stuff to work with now, thanks for the help.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 05:44 PM

Another quick question, Again I hope to just get pointed in the right direction. I need to figure out I want the files to move to a particular folder, I know how to do this, how do I get the file to originally upload, I have done all my original validation, but when I use
if(!is_uploaded_file($_FILES['file'])) {
$errorhandler .= "No file has been uploaded<br />";
$filemanager = false;
}
That sets the path for me to then do my extension checks, but the thing is it still gives me the error message, it works through my whole script, then at the end when it returns the error message, it returns the error messages from the fields that are blank, and then returns this message as well, even when I take a test file put it on there, and click submit. thanks, do I need to do something specific to get it to "upload" first.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 05:53 PM

Can you post the source of the form you're using to initiate the upload? The upload is usually done automagically and your script doesn't start until after the upload has finished.

Ken

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 05:54 PM

Try:
if(!is_uploaded_file($_FILES['file']['tmp_name'])){

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 05:56 PM

Perfect, she's running like a brand new car now. Thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#9 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:07 PM

Ok I must be missing something, the script runs perfectly, no syntax, or runtime errors. BUt something is going on with me checking the extensions, I tried pulling out .htm, .html files, and .jpg, and .txt files and it's still tellingme there not accepting that file type here is what I have for that portion of the script.

if(!is_uploaded_file($_FILES['file']['tmp_name'])){
        $errorhandler .= "No file has been uploaded<br />";
        $filemanager = false;
        }
        
if ($filemanager == true) {
$_accepted_extensions = array('.mpeg', '.mpg', '.wav', '.avi', '.mid', '.midi', '.doc', '.htm', '.jpg', '.jpeg', '.jfif', '.pdf', '.txt', '.wav', '.html', '.gif', '.qt', '.mp2', '.mp3');
    if ($filemanager == true) {
     if(in_array($_FILES['file'], $_accepted_extensions)) {
    $filemanager = true;
    $management = true;
    }else {
    $filemanager = false;
    $management = false;
    $errorhandler .= "You have attempted to upload the wrong file type<br />";
    $errorhandler .= "We only accept mpeg, mpg, wav, avi, mid, midi, doc, htm, jpg<br />";
    $errorhandler .= "jpeg, jfif, pdf, txt, wav, html, gif, qt, mp2, mp3 formats<br />";
    $errorhandler .= "To request new file types email<br />";
    $errorhandler .= "information@theyellowpagesnetwork.com";
                        }
                    }
                }
Do I have the in_array function set up right. The way I read it, is it's saying
if the $_accepted_extensions is shown in files then it will work.

I am on a unix apache server by the way.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#10 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 06:10 PM

Change:
if(in_array($_FILES['file'], $_accepted_extensions))

To:
if(in_array($_FILES['file']['name'], $_accepted_extensions))

Orio
Think you're smarty?

(Gone until 20 to November)

#11 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:14 PM

same problem, and file was the name of my form element, here is my form.
<form name="sendinformation" id="sendinformation" enctype="multipart/form-data" 
            action="apex/acceptfiles.php" method="post">
                <label for="type">What Type of Funny is it:</label>
                <select tabindex="1"name="type[]" id="type">
                <option>Video</option>
                <option>Picture</option>
                <option selected="selected">Joke</option>
                <option>Song</option>
                <option>Poem</option>
                <option>Story</option>
                </select><br />
                <label for="name">*Name the Funny:</label>
                <input tabindex="2" name="name" id="name" type="text" maxlength="80" /><br />
                <label for="keywords"><a href="keywords.htm">*Keywords:</a></label>
                <input tabindex="3" name="keywords" id="keywords" type="text" maxlength="80" /><br />
                <label for="file">*Upload your file here:</label>
                <input tabindex="4" name="file" id="file" type="file" /><br />
                <input tabindex="5" name="submit" id="submit" type="submit" value="Do It!" />
            </form>
The thing is, it properly tells me whether it has been uploaded or not, but when it starts trying to search through the array it's doing something wrong, I have something off somewhere, at that part of the script, it tells me that it didn't find a match, whether I use a proper extension or not.
It returns the message I have in the error string.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]You have attempted to upload the wrong file type
We only accept mpeg, mpg, wav, avi, mid, midi, doc, htm, jpg
jpeg, jfif, pdf, txt, wav, html, gif, qt, mp2, mp3 formats
To request new file types email
information@theyellowpagesnetwork.com[/quote]

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#12 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:43 PM

Also any advice per security as far as what files to accept or not, based on what I have is welcomed as well.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#13 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 06:44 PM

Ok, I dont know if there's an option using the pathinfo() function on uploaded files, but let's give it a try:
if ($filemanager == true) {
$_accepted_extensions = array('.mpeg', '.mpg', '.wav', '.avi', '.mid', '.midi', '.doc', '.htm', '.jpg', '.jpeg', '.jfif', '.pdf', '.txt', '.wav', '.html', '.gif', '.qt', '.mp2', '.mp3');
    if ($filemanager == true) {
     $extension_withdot=".";
     $info=pathinfo($_FILE['file']);
     $extension_withdot.=$info['extension'];
     if(in_array($extension_withdot, $_accepted_extensions)) {

//so on

Hope it'll work.

Orio.
Think you're smarty?

(Gone until 20 to November)

#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 06:52 PM

You need to use the pathinfo() function to extract the extension before you use the in_array() function.
Replace:
<?php if(in_array($_FILES['file'], $_accepted_extensions)) ?>
with
<?php
$tmp = pathinfo($_FILES['file']['name'];
if (in_array('.' . $tmp['extension'],$_accepted_extensions))
?>
I used "[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]'.' . $tmp['extension'][!--colorc--][/span][!--/colorc--]" since the pathinfo() function doesn't return the dot before the extension. Or you can remove the dot preceding each acceptable extension in your array.

Ken

#15 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:56 PM

Hmm it is still giving me the exact same improper functionality here is the new section of code.
if(!is_uploaded_file($_FILES['file']['tmp_name'])){
        $errorhandler .= "No file has been uploaded<br />";
        $filemanager = false;
        }
        
        if ($filemanager == true) {
$_accepted_extensions = array('.mpeg', '.mpg', '.wav', '.avi', '.mid', '.midi', '.doc', '.htm', '.jpg', '.jpeg', '.jfif', '.pdf', '.txt', '.wav', '.html', '.gif', '.qt', '.mp2', '.mp3');
    if ($filemanager == true) {
     $extension_withdot=".";
     $info=pathinfo($_FILE['file']);
     $extension_withdot.=$info['extension'];
     if(in_array($extension_withdot, $_accepted_extensions)) {
                    $filemanager = true;
                    $management = true;
                        }else {
                        $filemanager = false;
                        $management = false;
                        $errorhandler .= "You have attempted to upload the wrong file type<br />";
                        $errorhandler .= "We only accept mpeg, mpg, wav, avi, mid, midi, doc, htm, jpg<br />";
                        $errorhandler .= "jpeg, jfif, pdf, txt, wav, html, gif, qt, mp2, mp3 formats<br />";
                        $errorhandler .= "To request new file types email<br />";
                        $errorhandler .= "information@theyellowpagesnetwork.com";
                        }
                    }
                }
                    
                    if ($management == false || $filemanager == false) {
                    echo "{$errorhandler}";
                    }
That is the new code, with the same functionality, you can try it over at
[a href=\"http://www.funnyemailforwards.com\" target=\"_blank\"]http://www.funnyemailforwards.com[/a] and you will see that if you try to upload an accepted file it gives that message, or an unaccepted file. I tried a .txt, and .doc, and neither of them worked, but I tried the others and it gave me the same message.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#16 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 06:59 PM

My mistake, change:
$info=pathinfo($_FILES['file']);

To:
$info=pathinfo($_FILES['file']['name']);


Or use Ken's script, it's much nicer and should work [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Orio.

Edit- Changed $_FILE to $_FILES
Think you're smarty?

(Gone until 20 to November)

#17 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 06:59 PM

I am trying that other format that I saw from kenrbnsn currently as well.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 07:02 PM

The file name is in the $_FILES array not the $_FILE array.
Add this line to the start of your script to see what is being returned to your script:
<?php echo '<pre>' . print_r($_FILES,true) . '</pre>'; ?>

Ken

#19 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 June 2006 - 07:05 PM

I tried changing that line and it still didn't work, then I tried kens and it seemed to work. It works now, so I have a few questions, "learning purposes" what is this
$info=pathinfo($_FILE['file']['name']);
The reason the first didn't work is because you typo'd file, instead it's FILES, and I didn't catch that.
I am unfamiliar with this
I thought that $_FILE is what calls the array then the ['file'] should have been the name of the file in teh array, where does ['name'] come from because I didn't have anything in my script called name, and when I studied it, I foudn the references showing $_FILES['file'] and the file name would b where the 'file' is if the input was name upload it would be $_FILES['upload'] this part confused me.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#20 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 June 2006 - 07:10 PM

Read the section on [a href=\"http://www.php.net/manual/en/features.file-upload.php#features.file-upload.post-method\" target=\"_blank\"]uploading files[/a] in the PHP manual. It explains everything you need to know.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users