Jump to content

File Related


Ninjakreborn

Recommended Posts

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
[code]if(!is_uploaded_file($_FILES['file'])) {
$errorhandler .= "No file has been uploaded<br />";
$filemanager = false;
}[/code]
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.
Link to comment
Share on other sites

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.

[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) {
     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";
                        }
                    }
                }[/code]
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.
Link to comment
Share on other sites

same problem, and file was the name of my form element, here is my form.
[code]<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>[/code]
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]
Link to comment
Share on other sites

Ok, I dont know if there's an option using the pathinfo() function on uploaded files, but let's give it a try:
[code]
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[/code]

Hope it'll work.

Orio.
Link to comment
Share on other sites

You need to use the pathinfo() function to extract the extension before you use the in_array() function.
Replace:[code]<?php if(in_array($_FILES['file'], $_accepted_extensions)) ?>[/code]
with
[code]<?php
$tmp = pathinfo($_FILES['file']['name'];
if (in_array('.' . $tmp['extension'],$_accepted_extensions))
?>[/code]
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
Link to comment
Share on other sites

Hmm it is still giving me the exact same improper functionality here is the new section of code.
[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}";
                    }[/code]
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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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:
[code]<?php echo '<pre>' . print_r($_FILES,true) . '</pre>'; ?>[/code]

Ken
Link to comment
Share on other sites

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
[code]$info=pathinfo($_FILE['file']['name']);[/code]
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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

I have a question now. I have finished file validation and everything completely, now I am at the point where I need to do what I need to do, and database the information here is what I was wondering to get me off on the right step.
Here is the first thing I need to ask.
I know how to get the file to move over into a folder on the server once uploaded, I need to figure out some way to.
1. Record that url into a variable so I can database it(IN my same script so I can also record the time it was inserted(date).
2. How once I get all of it into the place, how to email it to him, my current idea, is to have it email him the link name, that way it's always what was entered, then if he declines, it removes the file from the database completely, if it is accepted then it is left alone, I think that would be easier, but what about the first part of that any suggestions.
I also want advice on this if anyone has any to offer, the current accepted file extensions I have written up are
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
'.mpeg', '.mpg', '.wav', '.avi', '.mid', '.midi', '.doc', '.htm', '.jpg', '.jpeg', '.jfif', '.pdf', '.txt', '.wav', '.html', '.gif', '.qt', '.mp2', '.mp3'[/quote]
Ideas, are any of these better to leave out, or any of them that I forgot that would be good.
I also need to figure out how to either have the files downloadable as links, or put in as embedded in the page, but I think embedding them based on the page wuold be hard, because I have to accomodate each possible filetype, any advice would be greatly appreciated, or anything pointing me in the proper direction, pointing me to the manual, about the section on files won't help, that's where my source of information has been coming from so far, that, google, and programming php version 1,(need version 2 later), so I have tools, that I have been using to help me learn faster, but the general concept of what I am trying to do is a little over my head, and I am trying to ease my way into it a little at a time.
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.